Class: GetText::TextDomain
- Inherits:
-
Object
- Object
- GetText::TextDomain
- Defined in:
- lib/gettext/text_domain.rb
Overview
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.
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
-
#mofiles ⇒ Object
readonly
Returns the value of attribute mofiles.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#output_charset ⇒ Object
Returns the value of attribute output_charset.
Class Method Summary collapse
-
.cached=(val) ⇒ Object
Set to cache the mo-file or not.
-
.cached? ⇒ Boolean
Cache the mo-file or not.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear cached mofiles.
-
#initialize(name, topdir = nil, output_charset = nil) ⇒ TextDomain
constructor
Creates a new GetText::TextDomain.
-
#translate_plural_message(lang, msgid, msgid_plural) ⇒ Object
Translates the translated string.
-
#translate_singular_message(lang, msgid) ⇒ Object
Translates the translated string.
Constructor Details
#initialize(name, topdir = nil, output_charset = nil) ⇒ TextDomain
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.
49 50 51 52 53 54 |
# File 'lib/gettext/text_domain.rb', line 49 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
#mofiles ⇒ Object (readonly)
Returns the value of attribute mofiles.
28 29 30 |
# File 'lib/gettext/text_domain.rb', line 28 def mofiles @mofiles end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
29 30 31 |
# File 'lib/gettext/text_domain.rb', line 29 def name @name end |
#output_charset ⇒ Object
Returns the value of attribute output_charset.
27 28 29 |
# File 'lib/gettext/text_domain.rb', line 27 def output_charset @output_charset end |
Class Method Details
.cached=(val) ⇒ Object
Set to cache the mo-file or not.
- val: true if cached, otherwise false.
40 41 42 |
# File 'lib/gettext/text_domain.rb', line 40 def self.cached=(val) @@cached = val end |
.cached? ⇒ Boolean
Cache the mo-file or not. Default is true. If $DEBUG is set then false.
34 35 36 |
# File 'lib/gettext/text_domain.rb', line 34 def self.cached? @@cached end |
Instance Method Details
#clear ⇒ Object
Clear cached mofiles.
135 136 137 |
# File 'lib/gettext/text_domain.rb', line 135 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.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/gettext/text_domain.rb', line 117 def (lang, msgid, msgid_plural) #:nodoc: key = msgid + "\000" + msgid_plural msg = (lang, key) ret = nil if ! msg ret = nil elsif msg.include?("\000") # [[msgstr[0], msgstr[1], msgstr[2],...], cond] mo = @mofiles[lang.to_s] cond = (mo and mo != :empty) ? mo.plural_as_proc : DEFAULT_PLURAL_CALC ret = [msg.split("\000", -1), cond] else ret = [[msg], DEFAULT_SINGLE_CALC] end ret end |
#translate_singular_message(lang, msgid) ⇒ Object
Translates the translated string.
- lang: Locale::Tag::Simple's subclass.
- msgid: the original message.
- Returns: the translated string or nil.
60 61 62 63 64 65 66 67 68 69 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 |
# File 'lib/gettext/text_domain.rb', line 60 def (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 |