123456789_123456789_123456789_123456789_123456789_

Class: RDoc::I18n::Locale

Relationships & Source Files
Inherits: Object
Defined in: lib/rdoc/i18n/locale.rb

Overview

A message container for a locale.

This object provides the following two features:

* Loads translated messages from .po file.
* Translates a message into the locale.

Class Method Summary

Instance Attribute Summary

  • #name readonly

    The name of the locale.

Instance Method Summary

Constructor Details

.new(name) ⇒ Locale

Creates a new locale object for #name locale. #name must follow IETF language tag format.

[ GitHub ]

  
# File 'lib/rdoc/i18n/locale.rb', line 48

def initialize(name)
  @name = name
  @messages = {}
end

Class Method Details

.[](locale_name)

Returns the locale object for locale_name.

[ GitHub ]

  
# File 'lib/rdoc/i18n/locale.rb', line 19

def [](locale_name)
  @@locales[locale_name] ||= new(locale_name)
end

.[]=(locale_name, locale)

Sets the locale object for locale_name.

Normally, this method is not used. This method is useful for testing.

[ GitHub ]

  
# File 'lib/rdoc/i18n/locale.rb', line 29

def []=(locale_name, locale)
  @@locales[locale_name] = locale
end

Instance Attribute Details

#name (readonly)

The name of the locale. It uses IETF language tag format href="_territory">language[.codeset]].

See also BCP 47 - Tags for Identifying Languages.

[ GitHub ]

  
# File 'lib/rdoc/i18n/locale.rb', line 42

attr_reader :name

Instance Method Details

#load(locale_directory)

Loads translation messages from locale_directory/+@name+/rdoc.po or locale_directory+/+@name.po. The former has high priority.

This method requires gettext gem for parsing .po file. If you don’t have gettext gem, this method doesn’t load .po file. This method warns and returns false.

Returns true if succeeded, false otherwise.

[ GitHub ]

  
# File 'lib/rdoc/i18n/locale.rb', line 63

def load(locale_directory)
  return false if @name.nil?

  po_file_candidates = [
    File.join(locale_directory, @name, 'rdoc.po'),
    File.join(locale_directory, "#{@name}.po"),
  ]
  po_file = po_file_candidates.find do |po_file_candidate|
    File.exist?(po_file_candidate)
  end
  return false unless po_file

  begin
    require 'gettext/po_parser'
    require 'gettext/mo'
  rescue LoadError
    warn('Need gettext gem for i18n feature:')
    warn('  gem install gettext')
    return false
  end

  po_parser = GetText::POParser.new
  messages = GetText::MO.new
  po_parser.report_warning = false
  po_parser.parse_file(po_file, messages)

  @messages.merge!(messages)

  true
end

#translate(message)

Translates the message into locale. If there is no translation messages for message in locale, message itself is returned.

[ GitHub ]

  
# File 'lib/rdoc/i18n/locale.rb', line 98

def translate(message)
  @messages[message] || message
end