Class: R18n::Backend
- Inherits:
-
Object
- Object
- R18n::Backend
- Includes:
- I18n::Backend::Transliterator
- Defined in:
- lib/r18n-rails-api/backend.rb
Overview
Constant Summary collapse
- RESERVED_KEYS =
[:scope, :default, :separator]
Instance Method Summary collapse
-
#available_locales ⇒ Object
Return array of available locales codes.
-
#localize(locale, object, format = :default, options = {}) ⇒ Object
Convert
objectto String, according to the rules of the current R18n locale. -
#reload! ⇒ Object
Reload R18n I18n object.
-
#translate(locale, key, options = {}) ⇒ Object
Find translation in R18n.
Instance Method Details
#available_locales ⇒ Object
Return array of available locales codes.
87 88 89 |
# File 'lib/r18n-rails-api/backend.rb', line 87 def available_locales R18n.available_locales.map { |i| i.code.to_sym } end |
#localize(locale, object, format = :default, options = {}) ⇒ Object
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.
76 77 78 79 80 81 82 83 84 |
# File 'lib/r18n-rails-api/backend.rb', line 76 def localize(locale, object, format = :default, = {}) 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! ⇒ Object
Reload R18n I18n object.
92 93 94 |
# File 'lib/r18n-rails-api/backend.rb', line 92 def reload! R18n.get.reload! end |
#translate(locale, key, options = {}) ⇒ Object
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.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/r18n-rails-api/backend.rb', line 37 def translate(locale, key, = {}) return key.map { |k| translate(locale, k, ) } if key.is_a?(Array) scope, default, separator = .values_at(*RESERVED_KEYS) params = .reject { |name, value| RESERVED_KEYS.include?(name) } result = lookup(locale, scope, key, separator, params) if result.is_a? Untranslated = .reject { |key, value| key == :default } default = [] if default.nil? default = [default] unless default.is_a? Array default.each do |entry| if entry.is_a? Symbol value = lookup(locale, scope, entry, separator, params) return value unless value.is_a? Untranslated elsif entry.is_a? Proc proc_key = .delete(:object) || key return entry.call(proc_key, ) else return entry end end raise ::I18n::MissingTranslationData.new(locale, key, ) else result end end |