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"
Direct Known Subclasses
Instance Method Summary collapse
-
#[](name, *params) ⇒ Object
(also: #method_missing)
Return translation with special
name
. -
#initialize(locale, path = '', options = {}) ⇒ Translation
constructor
This is internal a constructor.
-
#inspect ⇒ Object
Override inspect to easy debug.
-
#merge!(translations, locale) ⇒ Object
Add another hash with
translations
for somelocale
. -
#to_hash ⇒ Object
Return hash of current translation node.
-
#to_s ⇒ Object
Use untranslated filter to print path.
-
#translation_keys ⇒ Object
Return current translation keys.
-
#|(default) ⇒ Object
Return
default
.
Constructor Details
#initialize(locale, path = '', options = {}) ⇒ Translation
This is internal a constructor. To load translation use R18n::I18n.new(locales, translations_dir)
.
75 76 77 78 79 80 81 82 |
# File 'lib/r18n-core/translation.rb', line 75 def initialize(locale, path = '', = {}) @data = {} @locale = locale @path = path @filters = [:filters] || GlobalFilterList.instance merge! [:translations], [:locale] if [:translations] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ 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
.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/r18n-core/translation.rb', line 163 def [](name, *params) name = name.to_s if not name.is_a? String and not name.is_a? Fixnum value = @data[name] case value when TranslatedString @filters.process_string(:active, value, @path, params) when Typed @filters.process_typed(:active, value, params) when nil translated = @path.empty? ? '' : "#{@path}." Untranslated.new(translated, name, @locale, @filters) else value end end |
Instance Method Details
#[](name, *params) ⇒ Object Also known as: method_missing
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
.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/r18n-core/translation.rb', line 148 def [](name, *params) name = name.to_s if not name.is_a? String and not name.is_a? Fixnum value = @data[name] case value when TranslatedString @filters.process_string(:active, value, @path, params) when Typed @filters.process_typed(:active, value, params) when nil translated = @path.empty? ? '' : "#{@path}." Untranslated.new(translated, name, @locale, @filters) else value end end |
#inspect ⇒ Object
Override inspect to easy debug.
119 120 121 122 |
# File 'lib/r18n-core/translation.rb', line 119 def inspect path = @path.empty? ? 'root' : "`#{@path}`" "Translation #{path} for #{@locale.code} #{@data.inspect}" 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 110 |
# 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 => locale, :translations => value, :filters => @filters) when String c = { :locale => locale, :path => path } v = @filters.process_string(:passive, value, c, []) value = TranslatedString.new(v, locale, path, @filters) when Typed value.locale = locale value.path = path unless @filters.passive(value.type).empty? value = @filters.process_typed(:passive, value, { }) end end @data[name] = value elsif @data[name].is_a? Translation @data[name].merge! value, locale end end end |
#to_hash ⇒ Object
Return hash of current translation node.
132 133 134 135 136 137 |
# File 'lib/r18n-core/translation.rb', line 132 def to_hash Utils.hash_map(@data) do |key, value| value = value.to_hash if value.is_a? Translation [key, value] end end |
#to_s ⇒ Object
Use untranslated filter to print path.
113 114 115 116 |
# File 'lib/r18n-core/translation.rb', line 113 def to_s @filters.process(:all, Untranslated, @path, @locale, @path, [@path, '', @path]) end |
#translation_keys ⇒ Object
Return current translation keys.
Deprecated. Use to_hash.keys
.
127 128 129 |
# File 'lib/r18n-core/translation.rb', line 127 def translation_keys to_hash.keys end |
#|(default) ⇒ Object
Return default
.
140 141 142 |
# File 'lib/r18n-core/translation.rb', line 140 def |(default) default end |