Class: Releaf::I18nDatabase::TranslationsStore

Inherits:
Object
  • Object
show all
Includes:
Releaf::InstanceCache
Defined in:
app/lib/releaf/i18n_database/translations_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTranslationsStore

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_keysObject

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_atObject

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, options)
  if pluralizable_translation?(options)
    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

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 111

def auto_create?(key, options)
  return false unless config.i18n_database.translation_auto_creation
  return false if options[: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

Returns:

  • (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

Returns:

  • (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

#configObject



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, options)
  result = dig_translation(locale, translation_keys)
  if invalid_result?(locale, result, first_lookup, options)
    nil
  else
    result
  end
end

#exist?(key) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


50
51
52
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 50

def invalid_nonpluralized_result?(result, first_lookup, options)
  result.is_a?(Hash) && !first_lookup && !options.has_key?(:count)
end

#invalid_pluralized_result?(locale, result, options) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 54

def invalid_pluralized_result?(locale, result, options)
  result.is_a?(Hash) && options.has_key?(:count) && !valid_pluralized_result?(locale, options[:count], result)
end

#invalid_result?(locale, result, first_lookup, options) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 46

def invalid_result?(locale, result, first_lookup, options)
  invalid_nonpluralized_result?(result, first_lookup, options) || invalid_pluralized_result?(locale, result, options)
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_dataObject



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("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, options)
  translation_keys = key.split('.')

  (1..translation_keys.length).each do|i|
    result = dig_valid_translation(locale, translation_keys, i == 1, options)
    return result if returnable_result?(result, options)
    # 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, options)
  # mark translation as missing
  missing_keys["#{locale}.#{key}"] = true
  auto_create(key, options) if auto_create?(key, options)
end

#missing?(locale, key) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


138
139
140
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 138

def pluralizable_translation?(options)
  options.has_key?(:count) && options[:create_plurals] == true
end

#returnable_result?(result, options) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/lib/releaf/i18n_database/translations_store.rb', line 62

def returnable_result?(result, options)
  result.present? || options.fetch(:inherit_scopes, true) == false
end

#stored_keysObject



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_translationsObject



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

Returns:

  • (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