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 140
def clear
  @mofiles = {}
end
output_charset=(charset) click to toggle source

Set output_charset.

  • charset: output charset.

# File lib/gettext/runtime/textdomain.rb, line 146
def output_charset=(charset)
  @output_charset = charset
  clear
end
translate_singluar_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_singluar_message(lang, msgid)
  return "" if msgid == "" or msgid.nil?

  lang_key = lang.to_s

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

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

  msgstr = mofile[msgid]
  if msgstr and (msgstr.size > 0)
    msgstr
  elsif msgid.include?("\0000")
    # Check "aaa\000bbb" and show warning but return the singluar part.
    ret = nil
    msgid_single = msgid.split("\0000")[0]
    mofile.each{|key, val|
      if key =~ %r^#{Regexp.quote(msgid_single)}\0000/
        # Usually, this is not caused to make po-files from rgettext.
        warn %Q[Warning: n_("#{msgid_single}", "#{msgid.split("\000")[1]}") and n_("#{key.gsub(/\000/, '", "')}") are duplicated.]
        ret = val
        break
      end
    }
    ret
  else
    ret = nil
    mofile.each{|key, val|
      if key =~ %r^#{Regexp.quote(msgid)}\0000/
        ret = val.split("\0000")[0]
        break
      end
    }
    ret
  end
end