Class: I18n::Backend::ActiveRecord::Translation

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/i18n/backend/active_record/translation.rb

Constant Summary collapse

TRUTHY_CHAR =
"\001"
FALSY_CHAR =
"\002"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_localesObject



75
76
77
# File 'lib/i18n/backend/active_record/translation.rb', line 75

def available_locales
  Translation.select('DISTINCT locale').to_a.map { |t| t.locale.to_sym }
end

.locale(locale) ⇒ Object



58
59
60
# File 'lib/i18n/backend/active_record/translation.rb', line 58

def locale(locale)
  where(:locale => locale.to_s)
end

.lookup(keys, *separator) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/i18n/backend/active_record/translation.rb', line 62

def lookup(keys, *separator)
  column_name = connection.quote_column_name('key')
  keys = Array(keys).map! { |key| key.to_s }

  unless separator.empty?
    warn "[DEPRECATION] Giving a separator to Translation.lookup is deprecated. " <<
      "You can change the internal separator by overwriting FLATTEN_SEPARATOR."
  end

  namespace = "#{keys.last}#{I18n::Backend::Flatten::FLATTEN_SEPARATOR}%"
  where("#{column_name} IN (?) OR #{column_name} LIKE ?", keys, namespace)
end

.to_hashObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/i18n/backend/active_record/translation.rb', line 79

def to_hash
  Translation.all.each.with_object({}) do |t, memo|
    locale_hash = (memo[t.locale.to_sym] ||= {})
    keys = t.key.split('.')
    keys.each.with_index.inject(locale_hash) do |iterator, (key_part, index)|
      key = key_part.to_sym
      iterator[key] = keys[index + 1] ? (iterator[key] || {}) : t.value
      iterator[key]
    end
  end
end

Instance Method Details

#interpolates?(key) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/i18n/backend/active_record/translation.rb', line 92

def interpolates?(key)
  self.interpolations.include?(key) if self.interpolations
end

#valueObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/i18n/backend/active_record/translation.rb', line 96

def value
  value = read_attribute(:value)
  if is_proc
    Kernel.eval(value)
  elsif value == FALSY_CHAR
    false
  elsif value == TRUTHY_CHAR
    true
  else
    value
  end
end

#value=(value) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/i18n/backend/active_record/translation.rb', line 109

def value=(value)
  if value === false
    value = FALSY_CHAR
  elsif value === true
    value = TRUTHY_CHAR
  end

  write_attribute(:value, value)
end