Class: GetText::TextDomain
- Inherits:
-
Object
- Object
- GetText::TextDomain
- Defined in:
- lib/gettext/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
- CONFIG_PREFIX =
The default locale paths.
Config::CONFIG['prefix'].gsub(/\/local/, "")
- DEFAULT_LOCALE_PATHS =
[ "#{Config::CONFIG['datadir']}/locale/%{lang}/LC_MESSAGES/%{name}.mo", "#{Config::CONFIG['datadir'].gsub(/\/local/, "")}/locale/%{lang}/LC_MESSAGES/%{name}.mo", "#{CONFIG_PREFIX}/share/locale/%{lang}/LC_MESSAGES/%{name}.mo", "#{CONFIG_PREFIX}/local/share/locale/%{lang}/LC_MESSAGES/%{name}.mo" ].uniq
- @@cached =
! $DEBUG # Cache the mo-file or not. # Default is true. If $DEBUG is set then false.
Instance Attribute Summary collapse
-
#locale_paths ⇒ Object
readonly
Returns the value of attribute locale_paths.
-
#mofiles ⇒ Object
readonly
Returns the value of attribute mofiles.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#output_charset ⇒ Object
readonly
Returns the value of attribute output_charset.
Class Method Summary collapse
-
.add_default_locale_path(path) ⇒ Object
Add default locale path.
-
.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
-
#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_singluar_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 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.
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 |
# File 'lib/gettext/textdomain.rb', line 67 def initialize(name, topdir = nil, output_charset = nil) @name, @topdir, @output_charset = name, topdir, output_charset @locale_paths = [] if ENV["GETTEXT_PATH"] ENV["GETTEXT_PATH"].split(/,/).each {|i| @locale_paths += ["#{i}/%{lang}/LC_MESSAGES/%{name}.mo", "#{i}/%{lang}/%{name}.mo"] } elsif @topdir @locale_paths += ["#{@topdir}/%{lang}/LC_MESSAGES/%{name}.mo", "#{@topdir}/%{lang}/%{name}.mo"] end unless @topdir @locale_paths += DEFAULT_LOCALE_PATHS if defined? Gem $:.each do |path| if /(.*)\/lib$/ =~ path @locale_paths += [ "#{$1}/data/locale/%{lang}/LC_MESSAGES/%{name}.mo", "#{$1}/data/locale/%{lang}/%{name}.mo", "#{$1}/locale/%{lang}/%{name}.mo"] end end end end @mofiles = {} end |
Instance Attribute Details
#locale_paths ⇒ Object (readonly)
Returns the value of attribute locale_paths.
29 30 31 |
# File 'lib/gettext/textdomain.rb', line 29 def locale_paths @locale_paths end |
#mofiles ⇒ Object (readonly)
Returns the value of attribute mofiles.
30 31 32 |
# File 'lib/gettext/textdomain.rb', line 30 def mofiles @mofiles end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
31 32 33 |
# File 'lib/gettext/textdomain.rb', line 31 def name @name end |
#output_charset ⇒ Object (readonly)
Returns the value of attribute output_charset.
28 29 30 |
# File 'lib/gettext/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
58 59 60 |
# File 'lib/gettext/textdomain.rb', line 58 def self.add_default_locale_path(path) DEFAULT_LOCALE_PATHS.unshift(path) end |
.cached=(val) ⇒ Object
Set to cache the mo-file or not.
-
val: true if cached, otherwise false.
42 43 44 |
# File 'lib/gettext/textdomain.rb', line 42 def self.cached=(val) @@cached = val end |
.cached? ⇒ Boolean
Cache the mo-file or not. Default is true. If $DEBUG is set then false.
36 37 38 |
# File 'lib/gettext/textdomain.rb', line 36 def self.cached? @@cached end |
Instance Method Details
#clear ⇒ Object
169 170 171 |
# File 'lib/gettext/textdomain.rb', line 169 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.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/gettext/textdomain.rb', line 151 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] mofile = @mofiles[lang.to_posix.to_s] cond = (mofile and mofile != :empty) ? mofile.plural : nil cond ||= "n != 1" ret = [msg.split("\000"), cond] else ret = [[msg], "0"] 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.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/gettext/textdomain.rb', line 101 def (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 |