class GetText::TextDomain

GetText::TextDomain class manages mo-files of a textdomain.

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

add_default_locale_path(path) click to toggle source

Add default locale path. Usually you should use GetText.add_default_locale_path instead.

  • path: a new locale path. (e.g.) "/usr/share/locale/%{lang}/LC_MESSAGES/%{name}.mo" ('locale' => "ja_JP", 'name' => "textdomain")

  • Returns: the new DEFAULT_LOCALE_PATHS

# File lib/gettext/runtime/textdomain.rb, line 48
def self.add_default_locale_path(path)
  warn "Deprecated. Use GetText::LocalePath.add_default_rule instead."
  LocalePath.add_default_rule(path)
end
cached=(val) click to toggle source

Set to cache the mo-file or not.

  • val: true if cached, otherwise false.

# File lib/gettext/runtime/textdomain.rb, line 40
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/runtime/textdomain.rb, line 34
def self.cached?
  @@cached
end
new(name, topdir = nil, output_charset = nil) click to toggle source

Creates a new GetText::TextDomain.

  • name: the textdomain 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/runtime/textdomain.rb, line 58
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/runtime/textdomain.rb, line 144
def clear
  @mofiles = {}
end
output_charset=(charset) click to toggle source

Set output_charset.

  • charset: output charset.

# File lib/gettext/runtime/textdomain.rb, line 150
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/runtime/textdomain.rb, line 69
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 = %r^#{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(%r\0000/, '", "')
        duplicated_msgid = key.gsub(%r\0000/, '", "')
        warn("Warning: " +
              "n_(\"#{separated_msgid}\") and " +
              "n_(\"#{duplicated_msgid}\") " +
              "are duplicated.")
        ret = val
        break
      end
    end
  else
    msgid_prefix_re = %r^#{Regexp.quote(msgid)}\0000/
    mo.each do |key, val|
      if msgid_prefix_re =~ key
        ret = val.split("\0000")[0]
        break
      end
    end
  end
  ret
end