module Alchemy::I18n
Constants
- LOCALE_FILE_PATTERN
Public Class Methods
available_locales()
click to toggle source
# File lib/alchemy/i18n.rb, line 56 def available_locales Rails.application.config.i18n.available_locales || begin @@available_locales ||= nil @@available_locales || translation_files.collect { |f| f.match(LOCALE_FILE_PATTERN)[1].to_sym }.uniq.sort end end
available_locales=(locales)
click to toggle source
# File lib/alchemy/i18n.rb, line 65 def available_locales=(locales) @@available_locales = Array(locales).map(&:to_sym) @@available_locales = nil if @@available_locales.empty? end
translate(msg, **options)
click to toggle source
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
# 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
Private Class Methods
alchemy_scoped_scope(options)
click to toggle source
# File lib/alchemy/i18n.rb, line 82 def alchemy_scoped_scope(options) default_scope = ["alchemy"] case options[:scope] when Array default_scope + options[:scope] when String default_scope << options[:scope] when Symbol if options[:scope] != :alchemy default_scope << options[:scope] end else default_scope end end
humanize_default_string!(msg, options)
click to toggle source
# File lib/alchemy/i18n.rb, line 76 def humanize_default_string!(msg, options) return if options[:default].present? options[:default] = msg.is_a?(Symbol) ? msg.to_s.humanize : msg end
translation_files()
click to toggle source
# File lib/alchemy/i18n.rb, line 72 def translation_files ::I18n.load_path.select { |path| path.match(LOCALE_FILE_PATTERN) } end