Class: GetText::TextDomain

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext/runtime/textdomain.rb

Overview

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.

Constant Summary collapse

DEFAULT_PLURAL_CALC =
Proc.new{|n| n != 1}
DEFAULT_SINGLE_CALC =
Proc.new{|n| 0}
@@cached =
! $DEBUG
# Cache the mo-file or not.
# Default is true. If $DEBUG is set then false.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, topdir = nil, output_charset = nil) ⇒ TextDomain

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.



59
60
61
62
63
64
# File 'lib/gettext/runtime/textdomain.rb', line 59

def initialize(name, topdir = nil, output_charset = nil)
  @name, @output_charset = name, output_charset

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

Instance Attribute Details

#mofilesObject (readonly)

Returns the value of attribute mofiles.



29
30
31
# File 'lib/gettext/runtime/textdomain.rb', line 29

def mofiles
  @mofiles
end

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/gettext/runtime/textdomain.rb', line 30

def name
  @name
end

#output_charsetObject

Returns the value of attribute output_charset.



28
29
30
# File 'lib/gettext/runtime/textdomain.rb', line 28

def output_charset
  @output_charset
end

Class Method Details

.add_default_locale_path(path) ⇒ Object

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



49
50
51
52
# File 'lib/gettext/runtime/textdomain.rb', line 49

def self.add_default_locale_path(path)
  warn "Deprecated. Use GetText::LocalePath.add_default_rule instead."
  LocalePath.add_default_rule(path)
end

.cached=(val) ⇒ Object

Set to cache the mo-file or not.

  • val: true if cached, otherwise false.



41
42
43
# File 'lib/gettext/runtime/textdomain.rb', line 41

def self.cached=(val)
  @@cached = val
end

.cached?Boolean

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

Returns:

  • (Boolean)


35
36
37
# File 'lib/gettext/runtime/textdomain.rb', line 35

def self.cached?
  @@cached
end

Instance Method Details

#clearObject

Clear cached mofiles.



141
142
143
# File 'lib/gettext/runtime/textdomain.rb', line 141

def clear
  @mofiles = {}
end

#translate_plural_message(lang, msgid, msgid_plural) ⇒ Object

Translates the translated string.

  • lang: Locale::Tag::Simple’s subclass.

  • msgid: the original message.

  • msgid_plural: the original message(plural).

  • Returns: the translated string as an Array ([[msgstr1, msgstr2, …], cond]) or nil.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/gettext/runtime/textdomain.rb', line 123

def translate_plural_message(lang, msgid, msgid_plural)   #:nodoc:
  key = msgid + "\000" + msgid_plural
  msg = translate_singluar_message(lang, key)
  ret = nil
  if ! msg
    ret = nil
  elsif msg.include?("\000")
    # [[msgstr[0], msgstr[1], msgstr[2],...], cond]
    mofile = @mofiles[lang.to_posix.to_s]
    cond = (mofile and mofile != :empty) ? mofile.plural_as_proc : DEFAULT_PLURAL_CALC
    ret = [msg.split("\000"), cond]
  else
    ret = [[msg], DEFAULT_SINGLE_CALC]
  end
  ret
end

#translate_singluar_message(lang, msgid) ⇒ Object

Translates the translated string.

  • lang: Locale::Tag::Simple’s subclass.

  • msgid: the original message.

  • Returns: the translated string or nil.



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gettext/runtime/textdomain.rb', line 70

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?("\000")
    # Check "aaa\000bbb" and show warning but return the singluar part.
    ret = nil
    msgid_single = msgid.split("\000")[0]
    mofile.each{|key, val|
      if key =~ /^#{Regexp.quote(msgid_single)}\000/
        # 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 =~ /^#{Regexp.quote(msgid)}\000/
        ret = val.split("\000")[0]
        break
      end
    }
    ret
  end
end