Class: Mova::Translator
- Inherits:
-
Object
- Object
- Mova::Translator
- Includes:
- ReadStrategy::Lazy, Overridable
- Defined in:
- lib/mova/translator.rb
Overview
Wrapper around a storage that provides key management and fallbacks.
Translator knows that keys by definition are dot-separated and each key should include a locale. It also flattens any given hash, because ordinary key-value storage can handle only flat data.
Translator is in charge of returning non-empty data for given set of locales and keys, because it is likely that some translations are missing.
Defined Under Namespace
Modules: Overridable
Instance Attribute Summary collapse
-
#storage ⇒ #read, ...
readonly
Key-value storage for translations.
Instance Method Summary collapse
-
#get(key, locale, opts = {}) ⇒ String
Retrieves translation from the storage or return default value.
-
#put(translations) ⇒ void
Writes translations to the storage.
- #put!(translations) ⇒ void
Methods included from Overridable
#default, #initialize, #keys_to_try, #locales_to_try
Methods included from ReadStrategy::Lazy
Instance Attribute Details
#storage ⇒ #read, ... (readonly)
Key-value storage for translations.
18 19 20 |
# File 'lib/mova/translator.rb', line 18 def storage @storage end |
Instance Method Details
#get(key, locale, opts = {}) ⇒ String #get(keys, locale, opts = {}) ⇒ String #get(key, locales, opts = {}) ⇒ String #get(keys, locales, opts = {}) ⇒ String
Retrieves translation from the storage or return default value.
136 137 138 139 140 |
# File 'lib/mova/translator.rb', line 136 def get(key, locale, opts = {}) keys = resolve_scopes(key) locales = resolve_locales(locale) read_first(locales, keys) || opts[:default] || default(locales, keys, opts) end |