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
- GEM_PATH_RE =
:nodoc:
/(.*)\/lib$/
- CONFIG_PREFIX =
The default locale paths.
Config::CONFIG['prefix'].gsub(/\/local/, "")
- DEFAULT_LOCALE_PATHS =
[ "#{Config::CONFIG['datadir']}/locale/%{locale}/LC_MESSAGES/%{name}.mo", "#{Config::CONFIG['datadir'].gsub(/\/local/, "")}/locale/%{locale}/LC_MESSAGES/%{name}.mo", "#{CONFIG_PREFIX}/share/locale/%{locale}/LC_MESSAGES/%{name}.mo", "#{CONFIG_PREFIX}/local/share/locale/%{locale}/LC_MESSAGES/%{name}.mo" ].uniq
- @@check_mo =
false
Instance Attribute Summary collapse
-
#current_locale ⇒ Object
readonly
Returns the value of attribute current_locale.
-
#current_mo ⇒ Object
readonly
Returns the value of attribute current_mo.
-
#locale_paths ⇒ Object
readonly
Returns the value of attribute locale_paths.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.add_default_locale_path(path) ⇒ Object
Add default locale path.
-
.check_mo=(val) ⇒ Object
Sets to check mo-file or not.
-
.check_mo? ⇒ Boolean
Check mo-file is modified or not, and if mo-file is modified, reload mo-file again.
Instance Method Summary collapse
-
#gettext(msgid) ⇒ Object
Gets the translated string.
-
#initialize(name, topdir = nil, locale = nil) ⇒ TextDomain
constructor
Creates a new GetText::TextDomain.
-
#ngettext(msgid, msgid_plural, n) ⇒ Object
Gets the translated string.
-
#ngettext_data(msgid, msgid_plural) ⇒ Object
:nodoc:.
-
#same_property?(name, topdir, locale) ⇒ Boolean
Compare this object has the same name, topdir and locale.
-
#set_locale(locale, reload = false) ⇒ Object
Sets a new Locale::Object.
Constructor Details
#initialize(name, topdir = nil, locale = nil) ⇒ TextDomain
Creates a new GetText::TextDomain.
-
name: the textdomain name.
-
topdir: the locale path (“%topdir/%GetText.locale/LC_MESSAGES/%#name.mo”).
-
locale: the Locale::Object or nil.
-
Returns: a newly created GetText::TextDomain object.
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 |
# File 'lib/gettext/textdomain.rb', line 72 def initialize(name, topdir = nil, locale = nil) @name, @topdir = name, topdir @search_files = Array.new @locale_paths = [] if ENV["GETTEXT_PATH"] ENV["GETTEXT_PATH"].split(/,/).each {|i| @locale_paths += ["#{i}/%{locale}/LC_MESSAGES/%{name}.mo", "#{i}/%{locale}/%{name}.mo"] } elsif @topdir @locale_paths += ["#{@topdir}/%{locale}/LC_MESSAGES/%{name}.mo", "#{@topdir}/%{locale}/%{name}.mo"] end unless @topdir @locale_paths += DEFAULT_LOCALE_PATHS if defined? Gem $:.each do |path| if GEM_PATH_RE =~ path @locale_paths += [ "#{$1}/data/locale/%{locale}/LC_MESSAGES/%{name}.mo", "#{$1}/data/locale/%{locale}/%{name}.mo", "#{$1}/locale/%{locale}/%{name}.mo"] end end end end @mofiles = Hash.new set_locale(locale) end |
Instance Attribute Details
#current_locale ⇒ Object (readonly)
Returns the value of attribute current_locale.
29 30 31 |
# File 'lib/gettext/textdomain.rb', line 29 def current_locale @current_locale end |
#current_mo ⇒ Object (readonly)
Returns the value of attribute current_mo.
31 32 33 |
# File 'lib/gettext/textdomain.rb', line 31 def current_mo @current_mo end |
#locale_paths ⇒ Object (readonly)
Returns the value of attribute locale_paths.
30 31 32 |
# File 'lib/gettext/textdomain.rb', line 30 def locale_paths @locale_paths end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
32 33 34 |
# File 'lib/gettext/textdomain.rb', line 32 def name @name 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/%GetText.locale/LC_MESSAGES/%#name.mo” (‘locale’ => “ja_JP”, ‘name’ => “textdomain”)
-
Returns: the new DEFAULT_LOCALE_PATHS
63 64 65 |
# File 'lib/gettext/textdomain.rb', line 63 def self.add_default_locale_path(path) DEFAULT_LOCALE_PATHS.unshift(path) end |
.check_mo=(val) ⇒ Object
Sets to check mo-file or not. See GetText::TextDoman.check_mo? for more details.
-
val: true if “check mo” mode.
-
Returns: val
47 48 49 |
# File 'lib/gettext/textdomain.rb', line 47 def self.check_mo=(val) @@check_mo = val end |
.check_mo? ⇒ Boolean
Check mo-file is modified or not, and if mo-file is modified, reload mo-file again. This is effective in debug mode. Default is false. If $DEBUG is true, mo-file is checked even if this value is false.
-
Returns: true if “check mo” mode.
40 41 42 |
# File 'lib/gettext/textdomain.rb', line 40 def self.check_mo? @@check_mo end |
Instance Method Details
#gettext(msgid) ⇒ Object
Gets the translated string.
-
msgid: the original message.
-
Returns: the translated string or nil if not found.
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 117 def gettext(msgid) return "" if msgid == "" or msgid.nil? return nil unless @current_mo if @current_mo[msgid] and (@current_mo[msgid].size > 0) @current_mo[msgid] elsif msgid.include?("\000") ret = nil msgid_single = msgid.split("\000")[0] @current_mo.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.] if $DEBUG ret = val break end } ret else ret = nil @current_mo.each{|key, val| if key =~ /^#{Regexp.quote(msgid)}\000/ ret = val.split("\000")[0] break end } ret end end |
#ngettext(msgid, msgid_plural, n) ⇒ Object
Gets the translated string. (Deprecated. Don’t call this method directly)
-
msgid: the original message(single).
-
msgid: the original message(plural).
-
n: the number
-
Returns: the translated string or nil if not found.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/gettext/textdomain.rb', line 151 def ngettext(msgid, msgid_plural, n) key = msgid + "\000" + msgid_plural msg = gettext(key) if ! msg nil # do nothing. elsif msg == key msg = n == 1 ? msgid : msgid_plural elsif msg.include?("\000") ary = msg.split("\000") if @current_mo plural = eval(@current_mo.plural) if plural.kind_of?(Numeric) msg = ary[plural] else msg = plural ? ary[1] : ary[0] end else msg = n == 1 ? ary[0] : ary[1] end end msg end |
#ngettext_data(msgid, msgid_plural) ⇒ Object
:nodoc:
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/gettext/textdomain.rb', line 174 def ngettext_data(msgid, msgid_plural) #:nodoc: key = msgid + "\000" + msgid_plural msg = gettext(key) ret = nil if ! msg ret = nil elsif msg == key ret = nil elsif msg.include?("\000") # [[msgstr[0], msgstr[1], msgstr[2],...], cond] cond = @current_mo ? @current_mo.plural : nil cond ||= "n != 1" ret = [msg.split("\000"), cond] else ret = [[msg], "0"] end ret end |
#same_property?(name, topdir, locale) ⇒ Boolean
Compare this object has the same name, topdir and locale.
-
name: the textdomain name
-
topdir: the top directory of mo files or nil.
-
locale: the Locale::Object or nil.
-
Returns: true if this object has all of the same name, topdir and locale.
198 199 200 |
# File 'lib/gettext/textdomain.rb', line 198 def same_property?(name, topdir, locale) @name == name and @topdir == topdir and @current_locale == locale end |
#set_locale(locale, reload = false) ⇒ Object
Sets a new Locale::Object.
-
locale: a Locale::Object
-
reload: true if the mo-file is reloaded forcely
-
Returns: self
108 109 110 111 112 |
# File 'lib/gettext/textdomain.rb', line 108 def set_locale(locale, reload = false) @current_locale = locale load_mo(reload) self end |