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
- .scoped ⇒ 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
95 96 97 |
# File 'lib/i18n/backend/active_record/translation.rb', line 95 def available_locales select('DISTINCT locale').to_a.map { |t| t.locale.to_sym } end |
.locale(locale) ⇒ Object
70 71 72 |
# File 'lib/i18n/backend/active_record/translation.rb', line 70 def locale(locale) where(locale: locale.to_s) end |
.lookup(keys, *separator) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/i18n/backend/active_record/translation.rb', line 82 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 |
.scoped ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/i18n/backend/active_record/translation.rb', line 74 def scoped if (record_scope = ActiveRecord.config.scope) where(scope: record_scope) else all end end |
.to_h ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/i18n/backend/active_record/translation.rb', line 99 def to_h 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
112 113 114 |
# File 'lib/i18n/backend/active_record/translation.rb', line 112 def interpolates?(key) interpolations&.include?(key) end |
#invalidate_translations_cache ⇒ Object
140 141 142 |
# File 'lib/i18n/backend/active_record/translation.rb', line 140 def invalidate_translations_cache I18n.backend.reload! if I18n::Backend::ActiveRecord.config.cache_translations end |
#value ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/i18n/backend/active_record/translation.rb', line 116 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
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/i18n/backend/active_record/translation.rb', line 129 def value=(value) case value when false value = FALSY_CHAR when true value = TRUTHY_CHAR end write_attribute(:value, value) end |