Module: Alchemy::I18n

Defined in:
lib/alchemy/i18n.rb

Class Method Summary collapse

Class Method Details

.available_localesObject



47
48
49
50
# File 'lib/alchemy/i18n.rb', line 47

def self.available_locales
  @@available_locales ||= nil
  @@available_locales || translation_files.collect { |f| f.match(/.{2}\.yml$/).to_s.gsub(/\.yml/, '').to_sym }
end

.available_locales=(locales) ⇒ Object



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

def self.available_locales=(locales)
  @@available_locales = Array(locales).map { |locale| locale.to_sym }
  @@available_locales = nil if @@available_locales.empty?
end

.t(msg, *args) ⇒ Object

Alchemy translation methods

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

Alchemy::I18n.t("Hello World!", :default => 'Hello World!')

We define this method to define the value only once:

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

Note that interpolation still works:

Alchemy::I18n.t("Hello %{world}!", :world => @world)

It offers a shortcut method and view helper called _t

Notes

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

So a call for _t(‘hello’, :scope => :world) has to be translated like this:

de:
  alchemy:
    world:
      hello: Hallo


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/alchemy/i18n.rb', line 32

def self.t(msg, *args)
  options = args.extract_options!
  humanize_default_string!(msg, options)
  scope = ['alchemy']
  case options[:scope].class.name
  when "Array"
    scope += options[:scope]
  when "String"
    scope << options[:scope]
  when "Symbol"
    scope << options[:scope] unless options[:scope] == :alchemy
  end
  ::I18n.t(msg, options.merge(:scope => scope))
end

.translation_filesObject



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

def self.translation_files
  Dir.glob(File.join(File.dirname(__FILE__), '../../config/locales/alchemy.*.yml'))
end