class GetText::TextDomain
GetText::TextDomain
class manages mo-files of a text domain.
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
Public Class Methods
Set to cache the mo-file or not.
-
val: true if cached, otherwise false.
# File lib/gettext/text_domain.rb, line 39 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/text_domain.rb, line 33 def self.cached? @@cached end
Creates a new GetText::TextDomain
.
-
name: the text domain 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/text_domain.rb, line 48 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 cached mofiles.
# File lib/gettext/text_domain.rb, line 134 def clear @mofiles = {} end
Set output_charset.
-
charset: output charset.
# File lib/gettext/text_domain.rb, line 140 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/text_domain.rb, line 59 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?("\000") # Check "aaa\000bbb" and show warning but return the singular part. msgid_single = msgid.split("\000")[0] msgid_single_prefix_re = /^#{Regexp.quote(msgid_single)}\000/ 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(/\000/, '", "') duplicated_msgid = key.gsub(/\000/, '", "') warn("Warning: " + "n_(\"#{separated_msgid}\") and " + "n_(\"#{duplicated_msgid}\") " + "are duplicated.") ret = val break end end else plural_msgid_prefix = "#{msgid}\000" mo.each do |key, val| next unless Encoding.compatible?(key, plural_msgid_prefix) next unless key.start_with?(plural_msgid_prefix) ret = val.split("\000")[0] break end end ret end
Private Instance Methods
Load a mo-file from the file. lang is the subclass of Locale::Tag::Simple.
# File lib/gettext/text_domain.rb, line 148 def load_mo(lang) lang_key = lang.to_s mo = @mofiles[lang_key] if mo if mo == :empty return :empty elsif ! self.class.cached? mo.update! end return mo end path = @locale_path.current_path(lang) if path charset = @output_charset || lang.to_posix.charset || Locale.charset || "UTF-8" charset = normalize_charset(charset) @mofiles[lang_key] = MO.open(path, charset) else @mofiles[lang_key] = :empty end end
# File lib/gettext/text_domain.rb, line 172 def normalize_charset(charset) case charset when /\Autf8\z/i "UTF-8" else charset end end