module Mova::Translator::Overridable

Public Class Methods

new(opts = {}) click to toggle source

@param opts [Hash] @option opts [see storage] :storage default: {Storage::Memory} instance

# File lib/mova/translator.rb, line 23
def initialize(opts = {})
  @storage = opts.fetch(:storage) do
    require "mova/storage/memory"
    Storage::Memory.new
  end
end

Public Instance Methods

default(locales, keys, get_options) click to toggle source

@return [String] default value if no translation was found. @param locales [Array<String>] that were used to find a translation. @param keys [Array<String>] that were used to find a translation. @param get_options [Hash{Symbol => Object}] that were passed to {#get}

@example 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"
# File lib/mova/translator.rb, line 74
def default(locales, keys, get_options)
  EMPTY_TRANSLATION
end
keys_to_try(key) click to toggle source

@return [Array<String, Symbol>] keys that should be tried until non-empty

translation would be found.

@param key [String, Symbol]

@example 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"
# File lib/mova/translator.rb, line 58
def keys_to_try(key)
  [key]
end
locales_to_try(current_locale) click to toggle source

@return [Array<String, Symbol>] locales that should be tried until non-empty

translation would be found.

@param current_locale [String, Symbol]

@example 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"
# File lib/mova/translator.rb, line 42
def locales_to_try(current_locale)
  [current_locale]
end