Class: Mova::Translator

Inherits:
Object
  • Object
show all
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.

Since:

  • 0.1.0

Defined Under Namespace

Modules: Overridable

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Overridable

#default, #initialize, #keys_to_try, #locales_to_try

Methods included from ReadStrategy::Lazy

#read_first

Instance Attribute Details

#storage#read, ... (readonly)

Key-value storage for translations.

Returns:

  • (#read, #read_multi, #write, #exist?, #clear)

Since:

  • 0.1.0


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.

Examples:

translator.put(en: {hello: "world"})
translator.get("hello", :en) #=> "world"
translator.get("bye", :en) #=> ""

Providing the default if nothing found

translator.get("hello", :de, default: "nothing") #=> "nothing"

Overloads:

  • #get(key, locale, opts = {}) ⇒ String

    Parameters:

    • key (String, Symbol)
    • locale (String, Symbol)
  • #get(keys, locale, opts = {}) ⇒ String

    Examples:

    translator.put(en: {fail: "Fail"})
    translator.get(["big.fail", "mine.fail"], :en) #=> ""; tried "en.big.fail", then "en.mine.fail"

    Parameters:

  • #get(key, locales, opts = {}) ⇒ String

    Examples:

    translator.put(en: {hello: "world"})
    translator.get(:hello, :de) #=> ""; tried only "de.hello"
    translator.get(:hello, [:de, :en]) #=> "world"; tried "de.hello", then "en.hello"

    Disable locale fallbacks locally

    translator.put(en: {hello: "world"}) # suppose this instance has fallback to :en locale
    translator.get(:hello, :de) #=> "world"; tried "de.hello", then "en.hello"
    translator.get(:hello, [:de]) #=> ""; tried only "de.hello"

    Parameters:

  • #get(keys, locales, opts = {}) ⇒ String
    Note:

    Keys fallback has a higher priority than locales one, that is, Mova tries to find a translation for any given key and only then it fallbacks to another locale.

    Parameters:

    • keys (Array<String, Symbol>)
    • locales (Array<String, Symbol>)

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

Returns:

  • (String)

    translation or default value if nothing found

See Also:

Since:

  • 0.1.0


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

#put(translations) ⇒ void

This method returns an undefined value.

Writes translations to the storage.

Examples:

translator.put(en: {world: "world"}, uk: {world: "світ"})
translator.get("world", :uk) #=> "світ"

Parameters:

  • translations (Hash{String, Symbol => String, Hash})

    where root key/keys must be a locale

Since:

  • 0.1.0


151
152
153
154
155
# File 'lib/mova/translator.rb', line 151

def put(translations)
  Scope.flatten(translations).each do |key, value|
    storage.write(key, value) unless storage.exist?(key)
  end
end

#put!(translations) ⇒ void

Note:

This method overwrites existing translations.

This method returns an undefined value.

See Also:

Since:

  • 0.1.0


162
163
164
165
166
# File 'lib/mova/translator.rb', line 162

def put!(translations)
  Scope.flatten(translations).each do |key, value|
    storage.write(key, value)
  end
end