class R18n::Backend
R18n
backend for Rails
I18n. You must set R18n
I18n object before use this backend:
R18n.set locales, R18n::Loader::Rails I18n.l Time.now, format: :full #=> "6th of December, 2009 22:44"
Constants
- RESERVED_KEYS
Public Instance Methods
Return array of available locales codes.
# File lib/r18n-rails-api/backend.rb, line 88 def available_locales R18n.available_locales.map { |i| i.code.to_sym } end
Convert `object` to `String`, according to the rules of the current R18n
locale. It didn't use `locale` argument, only current R18n
I18n object. It support `Integer`, `Float`, `Time`, `Date` and `DateTime`.
Support Rails
I18n (`:default`, `:short`, `:long`, `:long_ordinal`, `:only_day` and `:only_second`) and R18n
(`:full`, `:human`, `:standard` and `:month`) time formatters.
# File lib/r18n-rails-api/backend.rb, line 77 def localize(locale, object, format = :default, _options = {}) i18n = get_i18n(locale) if format.is_a? Symbol key = format type = object.respond_to?(:sec) ? 'time' : 'date' format = i18n[type].formats[key] | format end i18n.localize(object, format) end
Reload R18n
I18n object.
# File lib/r18n-rails-api/backend.rb, line 93 def reload! R18n.get.reload! end
Find translation in R18n
. It didn't use `locale` argument, only current R18n
I18n object. Also it doesn't support `Proc` and variables in `default` String option.
# File lib/r18n-rails-api/backend.rb, line 37 def translate(locale, key, options = {}) return key.map { |k| translate(locale, k, options) } if key.is_a?(Array) scope, default, separator = options.values_at(*RESERVED_KEYS) params = options.reject { |name, _value| RESERVED_KEYS.include?(name) } result = lookup(locale, scope, key, separator, params) if result.is_a? Untranslated options = options.reject { |opts_key, _value| opts_key == :default } default = [] if default.nil? default = [default] unless default.is_a? Array default.each do |entry| case entry when Symbol value = lookup(locale, scope, entry, separator, params) return value unless value.is_a? Untranslated when Proc proc_key = options.delete(:object) || key return entry.call(proc_key, options) else return entry end end raise ::I18n::MissingTranslationData.new(locale, key, options) else result end end
Protected Instance Methods
# File lib/r18n-rails-api/backend.rb, line 105 def format_value(result) case result when TranslatedString result.to_s when UnpluralizedTranslation result.to_hash.transform_keys { |key| RailsPlural.from_r18n(key) } when Translation translation_to_hash(result) else result end end
# File lib/r18n-rails-api/backend.rb, line 99 def get_i18n(locale) i18n = R18n.get i18n = R18n.change(locale.to_s) if i18n.locale.code != locale.to_s i18n end
Find translation by `scope.key(params)` in current R18n
I18n object.
# File lib/r18n-rails-api/backend.rb, line 129 def lookup(locale, scope, key, separator, params) keys = (Array(scope) + Array(key)) .map { |k| k.to_s.split(separator || ::I18n.default_separator) } .flatten last = keys.pop.to_sym result = keys.inject(get_i18n(locale).t) do |node, iterated_key| if node.is_a? TranslatedString node.get_untranslated(iterated_key) else node[iterated_key] end end result = if result.is_a? TranslatedString result.get_untranslated(key) else result[last, params] end format_value(result) end
# File lib/r18n-rails-api/backend.rb, line 118 def translation_to_hash(translation) translation.to_hash.map do |key, value| [ key.to_sym, value.is_a?(Hash) ? translation_to_hash(value) : format_value(value) ] end.to_h end