Class: R18n::Translation
- Inherits:
-
Object
- Object
- R18n::Translation
- Defined in:
- lib/r18n-core/translation.rb
Overview
Translation is container of translated messages.
You can load several locales and if translation willn’t be found in first, r18n will be search it in next. Use R18n::I18n.new to load translations.
To get translation value use method with same name. If translation name is equal with Object methods (new
, to_s
, methods
) use [name, params…]
. If you want to get pluralizable value, just set value for pluralization in first argument of method. See samples below.
Translated strings will have locale
methods, which return Locale or UnsupportedLocale, if locale file isn’t exists.
Examples
translations/ru.yml
one: Один
translations/en.yml
one: One
two: Two
entry:
between: Between %1 and %2
methods: Is %1 method
comments: !!pl
0: no comments
1: one comment
n: %1 comments
example.rb
i18n.one #=> "Один"
i18n.two #=> "Two"
i18n.two.locale.code #=> "en"
i18n.two.locale.ltr? #=> "ltr"
i18n.entry.between(2, 3) #=> "between 2 and 3"
i18n['methods', 'object'] #=> "Is object method"
i18n.comments(0) #=> "no comments"
i18n.comments(10) #=> "10 comments"
Instance Method Summary collapse
-
#[](name, *params) ⇒ Object
Return translation with special
name
. -
#_keys ⇒ Object
Return current translation keys.
-
#initialize(main_locale, path = '', locale = nil, data = {}) ⇒ Translation
constructor
Create translation hash for
path
with messages indata
forlocale
. -
#merge!(translations, locale) ⇒ Object
Add another hash with
translations
for somelocale
. -
#method_missing(name, *params) ⇒ Object
Short and pretty way to get translation by method name.
-
#to_s ⇒ Object
Use untranslated filter to print path.
-
#|(default) ⇒ Object
Return
default
.
Constructor Details
#initialize(main_locale, path = '', locale = nil, data = {}) ⇒ Translation
Create translation hash for path
with messages in data
for locale
.
This is internal a constructor. To load translation use R18n::Translation.load(locales, translations_dir)
.
77 78 79 80 81 82 |
# File 'lib/r18n-core/translation.rb', line 77 def initialize(main_locale, path = '', locale = nil, data = {}) @path = path @data = {} @locale = main_locale merge! data, locale unless data.empty? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *params) ⇒ Object
Short and pretty way to get translation by method name. If translation has name like object methods (new
, to_s
, methods
) use []
method to access.
Translation can contain variable part. Just set is as %1
, %2
, etc in translations file and set values as methods params.
133 134 135 |
# File 'lib/r18n-core/translation.rb', line 133 def method_missing(name, *params) self[name, *params] end |
Instance Method Details
#[](name, *params) ⇒ Object
Return translation with special name
.
Translation can contain variable part. Just set is as %1
, %2
, etc in translations file and set values in next params
.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/r18n-core/translation.rb', line 141 def [](name, *params) value = @data[name.to_s] case value when TranslatedString Filters.process_string(Filters.active_enabled, value, @path, params) when Typed Filters.process(Filters.active_enabled, value.type, value.value, value.locale, value.path, params) when nil translated = @path.empty? ? '' : "#{@path}." Untranslated.new(translated, name.to_s, @locale) else value end end |
#_keys ⇒ Object
Return current translation keys.
118 119 120 |
# File 'lib/r18n-core/translation.rb', line 118 def _keys @data.keys end |
#merge!(translations, locale) ⇒ Object
Add another hash with translations
for some locale
. Current data is more priority, that new one in translations
.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/r18n-core/translation.rb', line 86 def merge!(translations, locale) translations.each_pair do |name, value| if not @data.has_key? name path = @path.empty? ? name : "#{@path}.#{name}" case value when Hash value = Translation.new(@locale, path, locale, value) when String v = TranslatedString.new(value, locale, path) value = Filters.process_string(Filters.passive_enabled, v, path, {}) when Typed value.locale = locale value.path = path unless Filters.passive_enabled[value.type].empty? value = Filters.process(Filters.passive_enabled, value.type, value.value, value.locale, value.path, {}) end end @data[name] = value elsif @data[name].is_a? Translation @data[name].merge! value, locale end end end |
#to_s ⇒ Object
Use untranslated filter to print path.
112 113 114 115 |
# File 'lib/r18n-core/translation.rb', line 112 def to_s Filters.process(Filters.enabled, Untranslated, @path, @locale, @path, [@path, '', @path]) end |
#|(default) ⇒ Object
Return default
.
123 124 125 |
# File 'lib/r18n-core/translation.rb', line 123 def |(default) default end |