Class: I18n::Backend::ActiveRecord::Translation
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- I18n::Backend::ActiveRecord::Translation
- Defined in:
- lib/i18n/backend/active_record/translation.rb
Constant Summary collapse
- TRUTHY_CHAR =
"\001"
- FALSY_CHAR =
"\002"
Class Method Summary collapse
- .available_locales ⇒ Object
- .locale(locale) ⇒ Object
- .lookup(keys, *separator) ⇒ Object
- .to_hash ⇒ Object
Instance Method Summary collapse
- #interpolates?(key) ⇒ Boolean
- #invalidate_translations_cache ⇒ Object
- #value ⇒ Object
- #value=(value) ⇒ Object
Class Method Details
.available_locales ⇒ Object
78 79 80 |
# File 'lib/i18n/backend/active_record/translation.rb', line 78 def available_locales Translation.select('DISTINCT locale').to_a.map { |t| t.locale.to_sym } end |
.locale(locale) ⇒ Object
61 62 63 |
# File 'lib/i18n/backend/active_record/translation.rb', line 61 def locale(locale) where(locale: locale.to_s) end |
.lookup(keys, *separator) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/i18n/backend/active_record/translation.rb', line 65 def lookup(keys, *separator) column_name = connection.quote_column_name('key') keys = Array(keys).map!(&: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_hash ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/i18n/backend/active_record/translation.rb', line 82 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] # rubocop:disable Lint/UnmodifiedReduceAccumulator end end end |
Instance Method Details
#interpolates?(key) ⇒ Boolean
95 96 97 |
# File 'lib/i18n/backend/active_record/translation.rb', line 95 def interpolates?(key) interpolations&.include?(key) end |
#invalidate_translations_cache ⇒ Object
123 124 125 |
# File 'lib/i18n/backend/active_record/translation.rb', line 123 def invalidate_translations_cache I18n.backend.reload! if I18n::Backend::ActiveRecord.config.cache_translations end |
#value ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/i18n/backend/active_record/translation.rb', line 99 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
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/i18n/backend/active_record/translation.rb', line 112 def value=(value) case value when false value = FALSY_CHAR when true value = TRUTHY_CHAR end write_attribute(:value, value) end |