Class: R18n::Translation

Inherits:
Object
  • Object
show all
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

UnpluralizedTranslation

Instance Method Summary collapse

Constructor Details

#initialize(locale, path = '', options = {}) ⇒ Translation

This is internal a constructor. To load translation use ‘R18n::I18n.new(locales, translations_dir)`.



72
73
74
75
76
77
78
79
# File 'lib/r18n-core/translation.rb', line 72

def initialize(locale, path = '', options = {})
  @data    = {}
  @locale  = locale
  @path    = path
  @filters = options[:filters] || GlobalFilterList.instance

  merge! options[:translations], options[:locale] if options[:translations]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject

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.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/r18n-core/translation.rb', line 166

def [](name, *params)
  unless [String, Integer, TrueClass, FalseClass]
      .any? { |klass| name.is_a?(klass) }
    name = name.to_s
  end
  value = @data[name]
  case value
  when TranslatedString
    path = @path.empty? ? name : "#{@path}.#{name}"
    @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.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/r18n-core/translation.rb', line 147

def [](name, *params)
  unless [String, Integer, TrueClass, FalseClass]
      .any? { |klass| name.is_a?(klass) }
    name = name.to_s
  end
  value = @data[name]
  case value
  when TranslatedString
    path = @path.empty? ? name : "#{@path}.#{name}"
    @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

#dig(*keys) ⇒ Object

Return translation located at keys.

See Also:

  • Hash#dig


170
171
172
# File 'lib/r18n-core/translation.rb', line 170

def dig(*keys)
  keys.reduce(self, :[])
end

#inspectObject

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

#itselfObject

I think we don’t need in the Ruby core method, but it can be handy as a key



176
177
178
# File 'lib/r18n-core/translation.rb', line 176

def itself
  self[__method__]
end

#merge!(translations, locale) ⇒ Object

Add another hash with translations for some locale. Current data is more priority, that new one in translations.



83
84
85
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 83

def merge!(translations, locale)
  (translations || {}).each_pair do |name, value|
    if !@data.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) && value.is_a?(Hash)
      @data[name].merge! value, locale
    end
  end
end

#to_hashObject

Return hash of current translation node.



132
133
134
135
136
# File 'lib/r18n-core/translation.rb', line 132

def to_hash
  @data.transform_values do |value|
    value.is_a?(Translation) ? value.to_hash : value
  end
end

#to_sObject

Use untranslated filter to print path.



112
113
114
115
116
# File 'lib/r18n-core/translation.rb', line 112

def to_s
  @filters.process(
    :all, Untranslated, @path, @locale, @path, [@path, '', @path]
  )
end

#translation_keysObject

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

#|(other) ⇒ Object

Return default.



139
140
141
# File 'lib/r18n-core/translation.rb', line 139

def |(other)
  other
end