Object
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.
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 47
47: def self.add_default_locale_path(path)
48: warn "Deprecated. Use GetText::LocalePath.add_default_rule instead."
49: LocalePath.add_default_rule(path)
50: end
Set to cache the mo-file or not.
val: true if cached, otherwise false.
# File lib/gettext/runtime/textdomain.rb, line 39
39: def self.cached=(val)
40: @@cached = val
41: end
Cache the mo-file or not. Default is true. If $DEBUG is set then false.
# File lib/gettext/runtime/textdomain.rb, line 33
33: def self.cached?
34: @@cached
35: end
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 57
57: def initialize(name, topdir = nil, output_charset = nil)
58: @name, @output_charset = name, output_charset
59:
60: @locale_path = LocalePath.new(@name, topdir)
61: @mofiles = {}
62: end
Clear cached mofiles.
# File lib/gettext/runtime/textdomain.rb, line 139
139: def clear
140: @mofiles = {}
141: end
Set output_charset.
charset: output charset.
# File lib/gettext/runtime/textdomain.rb, line 145
145: def output_charset=(charset)
146: @output_charset = charset
147: clear
148: end
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 68
68: def translate_singluar_message(lang, msgid)
69: return "" if msgid == "" or msgid.nil?
70:
71: lang_key = lang.to_s
72:
73: mofile = nil
74: if self.class.cached?
75: mofile = @mofiles[lang_key]
76: end
77: unless mofile
78: mofile = load_mo(lang)
79: end
80:
81: if (! mofile) or (mofile ==:empty)
82: return nil
83: end
84:
85: msgstr = mofile[msgid]
86: if msgstr and (msgstr.size > 0)
87: msgstr
88: elsif msgid.include?("\0000")
89: # Check "aaa\000bbb" and show warning but return the singluar part.
90: ret = nil
91: msgid_single = msgid.split("\0000")[0]
92: mofile.each{|key, val|
93: if key =~ /^#{Regexp.quote(msgid_single)}\0000/
94: # Usually, this is not caused to make po-files from rgettext.
95: warn %[Warning: n_("#{msgid_single}", "#{msgid.split("\000")[1]}") and n_("#{key.gsub(/\000/, '", "')}") are duplicated.]
96: ret = val
97: break
98: end
99: }
100: ret
101: else
102: ret = nil
103: mofile.each{|key, val|
104: if key =~ /^#{Regexp.quote(msgid)}\0000/
105: ret = val.split("\0000")[0]
106: break
107: end
108: }
109: ret
110: end
111: end
Load a mo-file from the file. lang is the subclass of Locale::Tag::Simple.
# File lib/gettext/runtime/textdomain.rb, line 153
153: def load_mo(lang)
154: lang = lang.to_posix unless lang.kind_of? Locale::Tag::Posix
155: lang_key = lang.to_s
156:
157: mofile = @mofiles[lang_key]
158: if mofile
159: if mofile == :empty
160: return :empty
161: elsif ! self.class.cached?
162: mofile.update!
163: end
164: return mofile
165: end
166:
167: path = @locale_path.current_path(lang)
168:
169: if path
170: charset = @output_charset || lang.charset || Locale.charset || "UTF-8"
171: @mofiles[lang_key] = MOFile.open(path, charset)
172: else
173: @mofiles[lang_key] = :empty
174: end
175: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.