Class: Releaf::I18nDatabase::TranslationsStore
- Inherits:
-
Object
- Object
- Releaf::I18nDatabase::TranslationsStore
- Includes:
- Releaf::InstanceCache
- Defined in:
- app/lib/releaf/i18n_database/translations_store.rb
Instance Attribute Summary collapse
-
#missing_keys ⇒ Object
Returns the value of attribute missing_keys.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Instance Method Summary collapse
- #auto_create(key, options) ⇒ Object
- #auto_create?(key, options) ⇒ Boolean
- #auto_creation_exception?(key) ⇒ Boolean
- #auto_creation_inclusion?(key) ⇒ Boolean
- #config ⇒ Object
- #dig_translation(locale, translation_keys) ⇒ Object
- #dig_valid_translation(locale, translation_keys, first_lookup, options) ⇒ Object
- #exist?(key) ⇒ Boolean
- #expired? ⇒ Boolean
-
#initialize ⇒ TranslationsStore
constructor
A new instance of TranslationsStore.
- #invalid_nonpluralized_result?(result, first_lookup, options) ⇒ Boolean
- #invalid_pluralized_result?(locale, result, options) ⇒ Boolean
- #invalid_result?(locale, result, first_lookup, options) ⇒ Boolean
- #key_hash(key) ⇒ Object
- #localization_data ⇒ Object
- #lookup(locale, key, options) ⇒ Object
- #missing(locale, key, options) ⇒ Object
- #missing?(locale, key) ⇒ Boolean
- #pluralizable_translation?(options) ⇒ Boolean
- #returnable_result?(result, options) ⇒ Boolean
- #stored_keys ⇒ Object
- #stored_translations ⇒ Object
- #valid_pluralized_result?(locale, count, result) ⇒ Boolean
Constructor Details
#initialize ⇒ TranslationsStore
Returns a new instance of TranslationsStore.
5 6 7 8 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 5 def initialize self.updated_at = Releaf::I18nDatabase::Backend.translations_updated_at self.missing_keys = {} end |
Instance Attribute Details
#missing_keys ⇒ Object
Returns the value of attribute missing_keys.
3 4 5 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 3 def missing_keys @missing_keys end |
#updated_at ⇒ Object
Returns the value of attribute updated_at.
3 4 5 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 3 def updated_at @updated_at end |
Instance Method Details
#auto_create(key, options) ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 128 def auto_create(key, ) if pluralizable_translation?() Releaf::I18nDatabase::Backend.locales_pluralizations.each do|pluralization| Releaf::I18nDatabase::I18nEntry.create(key: "#{key}.#{pluralization}") end else Releaf::I18nDatabase::I18nEntry.create(key: key) end end |
#auto_create?(key, options) ⇒ Boolean
111 112 113 114 115 116 117 118 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 111 def auto_create?(key, ) return false unless config.i18n_database.translation_auto_creation return false if [:auto_create] == false return false unless auto_creation_inclusion?(key) return false if auto_creation_exception?(key) return false if stored_keys.key?(key) true end |
#auto_creation_exception?(key) ⇒ Boolean
124 125 126 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 124 def auto_creation_exception?(key) config.i18n_database.translation_auto_creation_exclusion_patterns.find{|pattern| key.match(pattern) }.present? end |
#auto_creation_inclusion?(key) ⇒ Boolean
120 121 122 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 120 def auto_creation_inclusion?(key) config.i18n_database.translation_auto_creation_patterns.find{|pattern| key.match(pattern) }.present? end |
#config ⇒ Object
97 98 99 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 97 def config Releaf.application.config end |
#dig_translation(locale, translation_keys) ⇒ Object
31 32 33 34 35 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 31 def dig_translation(locale, translation_keys) translation_keys.inject(stored_translations[locale.to_sym]) do |h, key| h[key.to_sym] if h.is_a?(Hash) end end |
#dig_valid_translation(locale, translation_keys, first_lookup, options) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 37 def dig_valid_translation(locale, translation_keys, first_lookup, ) result = dig_translation(locale, translation_keys) if invalid_result?(locale, result, first_lookup, ) nil else result end end |
#exist?(key) ⇒ Boolean
14 15 16 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 14 def exist?(key) stored_keys.key? key end |
#expired? ⇒ Boolean
10 11 12 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 10 def expired? updated_at != Releaf::I18nDatabase::Backend.translations_updated_at end |
#invalid_nonpluralized_result?(result, first_lookup, options) ⇒ Boolean
50 51 52 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 50 def invalid_nonpluralized_result?(result, first_lookup, ) result.is_a?(Hash) && !first_lookup && !.has_key?(:count) end |
#invalid_pluralized_result?(locale, result, options) ⇒ Boolean
54 55 56 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 54 def invalid_pluralized_result?(locale, result, ) result.is_a?(Hash) && .has_key?(:count) && !valid_pluralized_result?(locale, [:count], result) end |
#invalid_result?(locale, result, first_lookup, options) ⇒ Boolean
46 47 48 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 46 def invalid_result?(locale, result, first_lookup, ) invalid_nonpluralized_result?(result, first_lookup, ) || invalid_pluralized_result?(locale, result, ) end |
#key_hash(key) ⇒ Object
89 90 91 92 93 94 95 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 89 def key_hash(key) config.all_locales.inject({}) do |h, locale| localized_key = "#{locale}.#{key}" locale_hash = key_locale_hash(localized_key, localization_data[localized_key]) h.merge(locale_hash) end end |
#localization_data ⇒ Object
67 68 69 70 71 72 73 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 67 def localization_data Releaf::I18nDatabase::I18nEntryTranslation .joins(:i18n_entry) .where.not(text: '') .pluck(Arel.sql("CONCAT(locale, '.', releaf_i18n_entries.key) AS translation_key"), "text") .to_h end |
#lookup(locale, key, options) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 18 def lookup(locale, key, ) translation_keys = key.split('.') (1..translation_keys.length).each do|i| result = dig_valid_translation(locale, translation_keys, i == 1, ) return result if returnable_result?(result, ) # remove second last value (going up to scope chain) translation_keys.delete_at(translation_keys.length - 2) end nil end |
#missing(locale, key, options) ⇒ Object
105 106 107 108 109 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 105 def missing(locale, key, ) # mark translation as missing missing_keys["#{locale}.#{key}"] = true auto_create(key, ) if auto_create?(key, ) end |
#missing?(locale, key) ⇒ Boolean
101 102 103 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 101 def missing?(locale, key) missing_keys.key? "#{locale}.#{key}" end |
#pluralizable_translation?(options) ⇒ Boolean
138 139 140 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 138 def pluralizable_translation?() .has_key?(:count) && [:create_plurals] == true end |
#returnable_result?(result, options) ⇒ Boolean
62 63 64 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 62 def returnable_result?(result, ) result.present? || .fetch(:inherit_scopes, true) == false end |
#stored_keys ⇒ Object
76 77 78 79 80 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 76 def stored_keys Releaf::I18nDatabase::I18nEntry.pluck(:key).inject({}) do|h, key| h.update(key => true) end end |
#stored_translations ⇒ Object
83 84 85 86 87 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 83 def stored_translations stored_keys.map do |key, _| key_hash(key) end.inject(&:deep_merge) || {} end |
#valid_pluralized_result?(locale, count, result) ⇒ Boolean
58 59 60 |
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 58 def valid_pluralized_result?(locale, count, result) result.key?(I18n.t(:'i18n.plural.rule', locale: locale, resolve: false).call(count)) end |