Class: RDoc::I18n::Locale (Ruby 2.5.7)
Description: Class : RDoc::I18n::Locale - Ruby 2.5.7

### Home Classes Methods

### In Files

- rdoc/i18n/locale.rb

### Parent

Object

### Methods

- ::[]
- ::[]=
- ::new
- #load
- #translate

### Class/Module Index

Quicksearch

- RDoc
- RDoc::Alias
- RDoc::AnonClass
- RDoc::AnyMethod
- RDoc::Attr
- RDoc::ClassModule
- RDoc::CodeObject
- RDoc::Comment
- RDoc::Constant
- RDoc::Context
- RDoc::Context::Section
- RDoc::CrossReference
- RDoc::ERBIO
- RDoc::ERBPartial
- RDoc::Encoding
- RDoc::Error
- RDoc::Extend
- RDoc::Generator
- RDoc::Generator::Darkfish
- RDoc::Generator::JsonIndex
- RDoc::Generator::Markup
- RDoc::Generator::POT
- RDoc::Generator::POT::MessageExtractor
- RDoc::Generator::POT::PO
- RDoc::Generator::POT::POEntry
- RDoc::Generator::RI
- RDoc::GhostMethod
- RDoc::I18n
- RDoc::I18n::Locale
- RDoc::I18n::Text
- RDoc::Include
- RDoc::Markdown
- RDoc::Markdown::Literals
- RDoc::Markup
- RDoc::Markup::AttrChanger
- RDoc::Markup::AttrSpan
- RDoc::Markup::AttributeManager
- RDoc::Markup::Attributes
- RDoc::Markup::BlankLine
- RDoc::Markup::BlockQuote
- RDoc::Markup::Document
- RDoc::Markup::Formatter
- RDoc::Markup::FormatterTestCase
- RDoc::Markup::HardBreak
- RDoc::Markup::Include
- RDoc::Markup::IndentedParagraph
- RDoc::Markup::List
- RDoc::Markup::ListItem
- RDoc::Markup::Paragraph
- RDoc::Markup::Parser
- RDoc::Markup::Parser::Error
- RDoc::Markup::Parser::ParseError
- RDoc::Markup::PreProcess
- RDoc::Markup::Raw
- RDoc::Markup::Rule
- RDoc::Markup::Special
- RDoc::Markup::TextFormatterTestCase
- RDoc::Markup::ToAnsi
- RDoc::Markup::ToBs
- RDoc::Markup::ToHtml
- RDoc::Markup::ToHtmlCrossref
- RDoc::Markup::ToHtmlSnippet
- RDoc::Markup::ToJoinedParagraph
- RDoc::Markup::ToLabel
- RDoc::Markup::ToMarkdown
- RDoc::Markup::ToRdoc
- RDoc::Markup::ToTableOfContents
- RDoc::Markup::ToTest
- RDoc::Markup::ToTtOnly
- RDoc::Markup::Verbatim
- RDoc::MetaMethod
- RDoc::MethodAttr
- RDoc::Mixin
- RDoc::NormalClass
- RDoc::NormalModule
- RDoc::Options
- RDoc::Parser
- RDoc::Parser::C
- RDoc::Parser::ChangeLog
- RDoc::Parser::Markdown
- RDoc::Parser::RD
- RDoc::Parser::Ruby
- RDoc::Parser::RubyTools
- RDoc::Parser::Simple
- RDoc::Parser::Text
- RDoc::RD
- RDoc::RD::BlockParser
- RDoc::RD::Inline
- RDoc::RD::InlineParser
- RDoc::RDoc
- RDoc::RI
- RDoc::RI::Driver
- RDoc::RI::Driver::Error
- RDoc::RI::Driver::NotFoundError
- RDoc::RI::Error
- RDoc::RI::Paths
- RDoc::RI::Task
- RDoc::Require
- RDoc::RipperStateLex
- RDoc::RipperStateLex::InnerStateLex
- RDoc::RubygemsHook
- RDoc::Servlet
- RDoc::SingleClass
- RDoc::Stats
- RDoc::Stats::Normal
- RDoc::Stats::Quiet
- RDoc::Stats::Verbose
- RDoc::Store
- RDoc::Store::Error
- RDoc::Store::MissingFileError
- RDoc::Task
- RDoc::TestCase
- RDoc::Text
- RDoc::TokenStream
- RDoc::TomDoc
- RDoc::TopLevel
- RDocTask

No matching classes.

# RDoc::I18n::Locale

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.

### Attributes

name[R]

The name of the locale. It uses IETF language tag format +[language[.codeset]]+.

See also BCP 47 - Tags for Identifying Languages.

### Public Class Methods

[](locale_name) click to toggle source

Returns the locale object for `locale_name`.

                   # File rdoc/i18n/locale.rb, line 19
    def [](locale_name)
      @@locales[locale_name] ||= new(locale_name)
    end

[]=(locale_name, locale) click to toggle source

Sets the locale object for `locale_name`.

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

                   # File rdoc/i18n/locale.rb, line 29
    def []=(locale_name, locale)
      @@locales[locale_name] = locale
    end

new(name) click to toggle source

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

                   # File rdoc/i18n/locale.rb, line 48
    def initialize(name)
      @name = name
      @messages = {}
    end

### Public Instance Methods

load(locale_directory) click to toggle source

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.

                   # File 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) click to toggle source

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

                   # File rdoc/i18n/locale.rb, line 98
    def translate(message)
      @messages[message] || message
    end