Module: Mova::Translator::Overridable

Included in:
Mova::Translator
Defined in:
lib/mova/translator.rb

Overview

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#default(locales, keys, get_options) ⇒ String

Returns default value if no translation was found.

Examples:

Override default value handling

translator = Mova::Translator.new.tap do |t|
  def t.default(locales, keys, get_options)
    "translation is missing"
  end
end
translator.get("hello", :de) #=> "translation is missing"

Parameters:

  • locales (Array<String>)

    that were used to find a translation.

  • keys (Array<String>)

    that were used to find a translation.

  • get_options (Hash{Symbol => Object})

    that were passed to Mova::Translator#get

Returns:

  • (String)

    default value if no translation was found.

Since:

  • 0.1.0


74
75
76
# File 'lib/mova/translator.rb', line 74

def default(locales, keys, get_options)
  EMPTY_TRANSLATION
end

#initialize(opts = {}) ⇒ Object

Parameters:

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

Options Hash (opts):

Since:

  • 0.1.0


23
24
25
26
27
28
# File 'lib/mova/translator.rb', line 23

def initialize(opts = {})
  @storage = opts.fetch(:storage) do
    require "mova/storage/memory"
    Storage::Memory.new
  end
end

#keys_to_try(key) ⇒ Array<String, Symbol>

Returns keys that should be tried until non-empty translation would be found.

Examples:

Override key fallbacks

translator = Mova::Translator.new.tap do |t|
  def t.keys_to_try(key)
    [key, "errors.#{key}"]
  end
end
translator.put(en: {errors: {fail: "Fail"}})
translator.get(:fail, :en) #=> "Fail"; tried "en.fail", then "en.errors.fail"

Parameters:

  • key (String, Symbol)

Returns:

  • (Array<String, Symbol>)

    keys that should be tried until non-empty translation would be found.

Since:

  • 0.1.0


58
59
60
# File 'lib/mova/translator.rb', line 58

def keys_to_try(key)
  [key]
end

#locales_to_try(current_locale) ⇒ Array<String, Symbol>

Returns locales that should be tried until non-empty translation would be found.

Examples:

Override locale fallbacks

translator = Mova::Translator.new.tap do |t|
  def t.locales_to_try(locale)
    [locale, :en]
  end
end
translator.put(en: {hello: "world"})
translator.get(:hello, :de) #=> "world"; tried "de.hello", then "en.hello"

Parameters:

  • current_locale (String, Symbol)

Returns:

  • (Array<String, Symbol>)

    locales that should be tried until non-empty translation would be found.

Since:

  • 0.1.0


42
43
44
# File 'lib/mova/translator.rb', line 42

def locales_to_try(current_locale)
  [current_locale]
end