Class: RDoc::I18n::Locale
- Inherits:
-
Object
- Object
- RDoc::I18n::Locale
- 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.
Constant Summary collapse
- @@locales =
:nodoc:
{}
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
The name of the locale.
Class Method Summary collapse
-
.[](locale_name) ⇒ Object
Returns the locale object for
locale_name. -
.[]=(locale_name, locale) ⇒ Object
Sets the locale object for
locale_name.
Instance Method Summary collapse
-
#initialize(name) ⇒ Locale
constructor
Creates a new locale object for
namelocale. -
#load(locale_directory) ⇒ Object
Loads translation messages from +locale_directory+/+@name+/rdoc.po or +locale_directory+/+@name+.po.
-
#translate(message) ⇒ Object
Translates the
messageinto locale.
Constructor Details
#initialize(name) ⇒ Locale
Creates a new locale object for name locale. name must
follow IETF language tag format.
50 51 52 53 |
# File 'lib/rdoc/i18n/locale.rb', line 50 def initialize(name) @name = name @messages = {} end |
Instance Attribute Details
#name ⇒ Object (readonly)
The name of the locale. It uses IETF language tag format
[language[_territory][.codeset][@modifier]].
See also BCP 47 - Tags for Identifying Languages.
44 45 46 |
# File 'lib/rdoc/i18n/locale.rb', line 44 def name @name end |
Class Method Details
.[](locale_name) ⇒ Object
Returns the locale object for locale_name.
21 22 23 |
# File 'lib/rdoc/i18n/locale.rb', line 21 def [](locale_name) @@locales[locale_name] ||= new(locale_name) end |
.[]=(locale_name, locale) ⇒ Object
Sets the locale object for locale_name.
Normally, this method is not used. This method is useful for testing.
31 32 33 |
# File 'lib/rdoc/i18n/locale.rb', line 31 def []=(locale_name, locale) @@locales[locale_name] = locale end |
Instance Method Details
#load(locale_directory) ⇒ Object
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.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rdoc/i18n/locale.rb', line 65 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 = GetText::MO.new po_parser.report_warning = false po_parser.parse_file(po_file, ) @messages.merge!() true end |
#translate(message) ⇒ Object
Translates the message into locale. If there is no translation
messages for message in locale, message itself is returned.
100 101 102 |
# File 'lib/rdoc/i18n/locale.rb', line 100 def translate() @messages[] || end |