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 48 def self.add_default_locale_path(path) warn "Deprecated. Use GetText::LocalePath.add_default_rule instead." LocalePath.add_default_rule(path) end
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
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
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
Clear cached mofiles.
# File lib/gettext/runtime/textdomain.rb, line 144 def clear @mofiles = {} end
Set output_charset.
charset: output charset.
# File lib/gettext/runtime/textdomain.rb, line 150 def output_charset=(charset) @output_charset = charset clear 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 69 def translate_singular_message(lang, msgid) return "" if 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 return mofile[msgid] if mofile.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/ mofile.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 msgid_prefix_re = /^#{Regexp.quote(msgid)}\0000/ mofile.each do |key, val| if msgid_prefix_re =~ key ret = val.split("\0000")[0] break end end end ret end
Generated with the Darkfish Rdoc Generator 2.