class CopyTunerClient::I18nBackend
This implementation will also load translations from locale files.
application. Instead, just use methods on the I18n class.
configured, so you will not need to instantiate this class from the
This backend will be used as the default I18n backend when the client is
Expects an object that acts like a Hash, responding to []
, []=
, and keys
.
I18n implementation designed to synchronize with CopyTuner.
def available_locales
-
(Array
- available locales)
def available_locales cached_locales = cache.keys.map { |key| key.split('.').first } (cached_locales + super).uniq.map { |locale| locale.to_sym } end
def default(locale, object, subject, options = {})
def default(locale, object, subject, options = {}) content = super(locale, object, subject, options) if content.respond_to?(:to_str) parts = I18n.normalize_keys(locale, object, options[:scope], options[:separator]) key = parts.join('.') cache[key] = content.to_str end content end
def initialize(cache)
-
cache
(Cache
) -- must act like a hash, returning and accept blurbs by key.
def initialize(cache) @cache = cache end
def load_translations(*filenames)
def load_translations(*filenames) super cache.wait_for_download end
def lookup(locale, key, scope = [], options = {})
def lookup(locale, key, scope = [], options = {}) parts = I18n.normalize_keys(locale, key, scope, options[:separator]) key_with_locale = parts.join('.') content = cache[key_with_locale] || super cache[key_with_locale] = "" if content.nil? content end
def store_item(locale, data, scope = [])
def store_item(locale, data, scope = []) if data.respond_to?(:to_hash) data.to_hash.each do |key, value| store_item(locale, value, scope + [key]) end elsif data.respond_to?(:to_str) key = ([locale] + scope).join('.') cache[key] = data.to_str end end
def store_translations(locale, data, options = {})
-
options
(Hash
) -- unused part of the I18n API -
data
(Hash
) -- nested key-value pairs to be added as blurbs -
locale
(String
) -- the locale (ie "en") to store translations for
def store_translations(locale, data, options = {}) super store_item(locale, data) end
def translate(locale, key, options = {})
-
(Object)
- the translated key (usually a String)
def translate(locale, key, options = {}) content = super(locale, key, options.merge(:fallback => true)) if CopyTunerClient.configuration.inline_translation content = (content.is_a?(Array) ? content : key.to_s) end if content.respond_to?(:html_safe) content.html_safe else content end end