Module: Alchemy::I18n

Defined in:
lib/alchemy/i18n.rb

Constant Summary collapse

LOCALE_FILE_PATTERN =
/alchemy\.(\S{2,5})\.yml/

Class Method Summary collapse

Class Method Details

.available_localesObject


56
57
58
59
60
61
# File 'lib/alchemy/i18n.rb', line 56

def available_locales
  @@available_locales ||= nil
  @@available_locales || translation_files.collect { |f|
    f.match(LOCALE_FILE_PATTERN)[1].to_sym
  }.uniq.sort
end

.available_locales=(locales) ⇒ Object


63
64
65
66
# File 'lib/alchemy/i18n.rb', line 63

def available_locales=(locales)
  @@available_locales = Array(locales).map(&:to_sym)
  @@available_locales = nil if @@available_locales.empty?
end

.translate(msg, **options) ⇒ Object

Alchemy translation methods

Instead of having to translate strings and defining a default value:

Alchemy::I18n.translate("Hello World!", default: 'Hello World!')

We define this method to define the value only once:

Alchemy::I18n.translate("Hello World!")

Note that interpolation still works:

Alchemy::I18n.translate("Hello %{world}!", world: @world)

Notes

All translations are scoped into the alchemy namespace. Even scopes are scoped into the alchemy namespace.

So a call for Alchemy::translate('hello', scope: 'world') has to be translated like this:

de:
  alchemy:
    world:
      hello: Hallo

50
51
52
53
54
# File 'lib/alchemy/i18n.rb', line 50

def translate(msg, **options)
  humanize_default_string!(msg, options)
  scope = alchemy_scoped_scope(options)
  ::I18n.t(msg, **options.merge(scope: scope))
end