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_h ⇒ Object
Instance Method Summary collapse
- #interpolates?(key) ⇒ Boolean
- #invalidate_translations_cache ⇒ Object
- #value ⇒ Object
- #value=(value) ⇒ Object
Class Method Details
.available_locales ⇒ Object
84 85 86 |
# File 'lib/i18n/backend/active_record/translation.rb', line 84 def available_locales Translation.select('DISTINCT locale').to_a.map { |t| t.locale.to_sym } end |
.locale(locale) ⇒ Object
67 68 69 |
# File 'lib/i18n/backend/active_record/translation.rb', line 67 def locale(locale) where(locale: locale.to_s) end |
.lookup(keys, *separator) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/i18n/backend/active_record/translation.rb', line 71 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_h ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/i18n/backend/active_record/translation.rb', line 88 def to_h 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
101 102 103 |
# File 'lib/i18n/backend/active_record/translation.rb', line 101 def interpolates?(key) interpolations&.include?(key) end |
#invalidate_translations_cache ⇒ Object
129 130 131 |
# File 'lib/i18n/backend/active_record/translation.rb', line 129 def invalidate_translations_cache I18n.backend.reload! if I18n::Backend::ActiveRecord.config.cache_translations end |
#value ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/i18n/backend/active_record/translation.rb', line 105 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
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/i18n/backend/active_record/translation.rb', line 118 def value=(value) case value when false value = FALSY_CHAR when true value = TRUTHY_CHAR end write_attribute(:value, value) end |