Parent

R18n::Translation

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"

Public Class Methods

new(locale, path = '', options = {}) click to toggle source

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

# File lib/r18n-core/translation.rb, line 74
def initialize(locale, path = '', options = {})
  @data    = {}
  @locale  = locale
  @path    = path
  @filters = options[:filters] || GlobalFilterList.instance

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

Public Instance Methods

[](name, *params) click to toggle source

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.

# File lib/r18n-core/translation.rb, line 147
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
    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
Also aliased as: method_missing
inspect() click to toggle source

Override inspect to easy debug.

# File lib/r18n-core/translation.rb, line 118
def inspect
  path = @path.empty? ? 'root' : "`#{@path}`"
  "Translation #{path} for #{@locale.code} #{@data.inspect}"
end
merge!(translations, locale) click to toggle source

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

# File lib/r18n-core/translation.rb, line 85
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
method_missing(name, *params) click to toggle source
Alias for: []
to_hash() click to toggle source

Return hash of current translation node.

# File lib/r18n-core/translation.rb, line 131
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() click to toggle source

Use untranslated filter to print path.

# File lib/r18n-core/translation.rb, line 112
def to_s
  @filters.process(:all, Untranslated, @path, @locale, @path,
                  [@path, '', @path])
end
translation_keys() click to toggle source

Return current translation keys.

Deprecated. Use to_hash.keys.

# File lib/r18n-core/translation.rb, line 126
def translation_keys
  to_hash.keys
end
|(default) click to toggle source

Return default.

# File lib/r18n-core/translation.rb, line 139
def |(default)
  default
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.