Class: GetText::TextDomain

### Home Classes Methods

### In Files

- lib/gettext/text_domain.rb

### Parent

Object

### Methods

- ::cached=
- ::cached?
- ::new
- #clear
- #output_charset=
- #translate_singular_message

### Class/Module Index

Quicksearch

- GetText
- GetText::ClassInfo
- GetText::ErbParser
- GetText::GladeParser
- GetText::LocalePath
- GetText::MO
- GetText::MO::HeaderRev1
- GetText::MO::InvalidFormat
- GetText::NoboundTextDomainError
- GetText::PO
- GetText::PO::NonExistentEntryError
- GetText::POEntry
- GetText::POEntry::Formatter
- GetText::POEntry::InvalidTypeError
- GetText::POEntry::NoMsgctxtError
- GetText::POEntry::NoMsgidError
- GetText::POEntry::NoMsgidPluralError
- GetText::POEntryForRubyParser
- GetText::POFormat
- GetText::POParser
- GetText::ParseError
- GetText::RubyParser
- GetText::TextDomain
- GetText::TextDomainGroup
- GetText::TextDomainManager
- GetText::Tools
- GetText::Tools::MsgCat
- GetText::Tools::MsgCat::Config
- GetText::Tools::MsgCat::Merger
- GetText::Tools::MsgInit
- GetText::Tools::MsgInit::ArgumentError
- GetText::Tools::MsgInit::Error
- GetText::Tools::MsgInit::ValidationError
- GetText::Tools::MsgMerge
- GetText::Tools::MsgMerge::Config
- GetText::Tools::MsgMerge::FuzzyEntryFinder
- GetText::Tools::MsgMerge::Merger
- GetText::Tools::Task
- GetText::Tools::Task::Error
- GetText::Tools::Task::Path
- GetText::Tools::Task::ValidationError
- GetText::Tools::XGetText

No matching classes.

# GetText::TextDomain

GetText::TextDomain class manages mo-files of a text domain.

Usually, you don't need to use this class directly.

Notice: This class is unstable. APIs will be changed.

### Constants

DEFAULT_PLURAL_CALC
DEFAULT_SINGLE_CALC

### Attributes

mofiles[R]
name[R]
output_charset[R]

### Public Class Methods

cached=(val) click to toggle source

Set to cache the mo-file or not.

- val: true if cached, otherwise false.

    # File lib/gettext/text_domain.rb, line 39
    def self.cached=(val)
      @@cached = val
    end

cached?() click to toggle source

Cache the mo-file or not. Default is true. If $DEBUG is set then false.

    # File lib/gettext/text_domain.rb, line 33
    def self.cached?
      @@cached
    end

new(name, topdir = nil, output_charset = nil) click to toggle source

Creates a new GetText::TextDomain.

- name: the text domain name.

- topdir: the locale path ("%{topdir}/%{lang}/LC_MESSAGES/%{name}.mo") or nil.

- output_charset: output charset.

- Returns: a newly created GetText::TextDomain object.

    # File lib/gettext/text_domain.rb, line 48
    def initialize(name, topdir = nil, output_charset = nil)
      @name, @output_charset = name, output_charset

      @locale_path = LocalePath.new(@name, topdir)
      @mofiles = {}
    end

### Public Instance Methods

clear() click to toggle source

Clear cached mofiles.

    # File lib/gettext/text_domain.rb, line 134
    def clear
      @mofiles = {}
    end

output_charset=(charset) click to toggle source

Set output_charset.

- charset: output charset.

    # File lib/gettext/text_domain.rb, line 140
    def output_charset=(charset)
      @output_charset = charset
      clear
    end

translate_singular_message(lang, msgid) click to toggle source

Translates the translated string.

- lang: Locale::Tag::Simple's subclass.

- msgid: the original message.

- Returns: the translated string or nil.

    # File lib/gettext/text_domain.rb, line 59
    def translate_singular_message(lang, msgid)
      return "" if msgid.nil?

      lang_key = lang.to_s

      mo = nil
      if self.class.cached?
        mo = @mofiles[lang_key]
      end
      unless mo
        mo = load_mo(lang)
      end

      if (! mo) or (mo ==:empty)
        return nil
      end

      return mo[msgid] if mo.has_key?(msgid)

      ret = nil
      if msgid.include?("\0000")
        # Check "aaa\000bbb" and show warning but return the singular part.
        msgid_single = msgid.split("\0000")[0]
        msgid_single_prefix_re = /^#{Regexp.quote(msgid_single)}\0000/
        mo.each do |key, val|
          if msgid_single_prefix_re =~ key
            # Usually, this is not caused to make po-files from rgettext.
            separated_msgid = msgid.gsub(/\0000/, '", "')
            duplicated_msgid = key.gsub(/\0000/, '", "')
            warn("Warning: " +
                  "n_(\"#{separated_msgid}\") and " +
                  "n_(\"#{duplicated_msgid}\") " +
                  "are duplicated.")
            ret = val
            break
          end
        end
      else
        plural_msgid_prefix = "#{msgid}\0000"
        mo.each do |key, val|
          next unless Encoding.compatible?(key, plural_msgid_prefix)
          next unless key.start_with?(plural_msgid_prefix)
          ret = val.split("\0000")[0]
          break
        end
      end
      ret
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.