module I18n::Backend::Weeler::Implementation
Public Instance Methods
reload_cache()
click to toggle source
# File lib/i18n/backend/weeler.rb, line 49 def reload_cache i18n_cache.clear Translation.all.each do |translation| # Load in cache unless value is nil or it is blank and empty # translation acts like missing if !(translation.value.nil? || (::Weeler.empty_translation_acts_like_missing && translation.value.blank?)) i18n_cache.write [translation.locale, translation.key], translation end end i18n_cache.write('UPDATED_AT', Settings.i18n_updated_at) if ActiveRecord::Base.connection.data_source_exists?('settings') end
store_translations(locale, data, options = {})
click to toggle source
# File lib/i18n/backend/weeler.rb, line 41 def store_translations(locale, data, options = {}) escape = options.fetch(:escape, true) flatten_translations(locale, data, escape, false).each do |key, value| Translation.locale(locale).lookup(expand_keys(key)).delete_all Translation.create(locale: locale.to_s, key: key.to_s, value: value) end end
Protected Instance Methods
lookup(locale, key, scope = [], options = {})
click to toggle source
# File lib/i18n/backend/weeler.rb, line 65 def lookup(locale, key, scope = [], options = {}) return fallback_backend_translation locale, key if key.to_s.start_with?('i18n') key = normalize_flat_keys(locale, key, scope, options[:separator]) return lookup_in_cache(locale, key, scope, options) end
Private Instance Methods
expand_keys(not_expanded_key)
click to toggle source
For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']
# File lib/i18n/backend/weeler.rb, line 135 def expand_keys(not_expanded_key) not_expanded_key.to_s.split(FLATTEN_SEPARATOR).inject([]) do |keys, key| keys << [keys.last, key].compact.join(FLATTEN_SEPARATOR) end end
fallback_backend_translation(locale, key)
click to toggle source
# File lib/i18n/backend/weeler.rb, line 166 def fallback_backend_translation locale, key if I18n.backend.backends.size > 1 alternative_backend = I18n.backend.backends[1] alternative_backend.send(:lookup, locale, key) else nil end end
lookup_in_cache(locale, key, scope = [], options = {})
click to toggle source
# File lib/i18n/backend/weeler.rb, line 73 def lookup_in_cache locale, key, scope = [], options = {} # reload cache if cache timestamp differs from last translations update reload_cache if ((!ActiveRecord::Base.connection.data_source_exists?('settings')) || i18n_cache.read('UPDATED_AT') != Settings.i18n_updated_at) # log locale/key usage for statistics if Settings.log_key_usage == 'true' i18n_cache.delete([:dump_usage_stats, Process.pid]) log_key_usage(locale, key) end if Settings.log_key_usage == 'dump' dump_key_usage end return nil if i18n_cache.read([:missing, [locale, key]]) keys = expand_keys key keys.reverse.each do |check_key| result = i18n_cache.read([locale, check_key]) return result.value unless result.blank? end # mark translation as missing i18n_cache.write([:missing, [locale, key]], true) if ::Weeler.create_missing_translations return store_empty_translation locale, key, options else return nil end end
lookup_in_database(locale, key, scope = [], options = {})
click to toggle source
# File lib/i18n/backend/weeler.rb, line 108 def lookup_in_database locale, key, scope = [], options = {} result = Translation.locale(locale).lookup(key).load if result.nil? || (::Weeler.empty_translation_acts_like_missing && result.blank?) if ::Weeler.create_missing_translations return store_empty_translation locale, key, options else return nil end elsif result.first.key == key translation = result.first if translation.value.blank? fallback_value = fallback_backend_translation locale, key translation.update value: fallback_value if fallback_value.present? end return translation.value else chop_range = (key.size + FLATTEN_SEPARATOR.size)..-1 result = result.inject({}) do |hash, r| hash[r.key.slice(chop_range)] = r.value hash end return result.deep_symbolize_keys end end
store_empty_translation(locale, singular_key, options)
click to toggle source
Store single empty translation
# File lib/i18n/backend/weeler.rb, line 142 def store_empty_translation locale, singular_key, options return_value = nil interpolations = options.keys - I18n::RESERVED_KEYS keys = options[:count] ? PLURAL_KEYS.map { |k| [singular_key, k].join(FLATTEN_SEPARATOR) } : [singular_key] keys.each do |key| translation = Weeler::Translation.find_or_initialize_by locale: locale.to_s, key: key translation.interpolations = interpolations fallback_value = fallback_backend_translation locale, key if fallback_value.present? translation.value = fallback_value translation.save reload_cache else translation.save end return_value = translation.value end return_value end