Class GetText::TextDomain
In: lib/gettext/textdomain.rb
Parent: 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.

Methods

Constants

CONFIG_PREFIX = Config::CONFIG['prefix'].gsub(/\/local/, "")   The default locale paths.
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"

Attributes

current_locale  [R] 
current_mo  [R] 
locale_paths  [R] 
name  [R] 

Public Class methods

Add default locale path. Usually you should use GetText.add_default_locale_path instead.

  • path: a new locale path. (e.g.) "/usr/share/locale/%{locale}/LC_MESSAGES/%{name}.mo" (‘locale’ => "ja_JP", ‘name’ => "textdomain")
  • Returns: the new DEFAULT_LOCALE_PATHS

[Source]

    # File lib/gettext/textdomain.rb, line 63
63:     def self.add_default_locale_path(path)
64:       DEFAULT_LOCALE_PATHS.unshift(path)
65:     end

Sets to check mo-file or not. See GetText::TextDoman.check_mo? for more details.

  • val: true if "check mo" mode.
  • Returns: val

[Source]

    # File lib/gettext/textdomain.rb, line 47
47:     def self.check_mo=(val)
48:       @@check_mo = val
49:     end

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.

[Source]

    # File lib/gettext/textdomain.rb, line 40
40:     def self.check_mo?
41:       @@check_mo
42:     end

Creates a new GetText::TextDomain.

  • name: the textdomain name.
  • topdir: the locale path ("%{topdir}/%{locale}/LC_MESSAGES/%{name}.mo").
  • locale: the Locale::Object or nil.
  • Returns: a newly created GetText::TextDomain object.

[Source]

     # File lib/gettext/textdomain.rb, line 72
 72:     def initialize(name, topdir = nil, locale = nil)
 73:       @name, @topdir = name, topdir
 74:       @search_files = Array.new
 75: 
 76:       @locale_paths = []
 77:       if ENV["GETTEXT_PATH"]
 78:         ENV["GETTEXT_PATH"].split(/,/).each {|i| 
 79:       @locale_paths += ["#{i}/%{locale}/LC_MESSAGES/%{name}.mo", "#{i}/%{locale}/%{name}.mo"]
 80:     }
 81:       elsif @topdir
 82:         @locale_paths += ["#{@topdir}/%{locale}/LC_MESSAGES/%{name}.mo", "#{@topdir}/%{locale}/%{name}.mo"]
 83:       end
 84: 
 85:       unless @topdir
 86:     @locale_paths += DEFAULT_LOCALE_PATHS
 87:     
 88:     if defined? Gem
 89:       $:.each do |path|
 90:         if GEM_PATH_RE =~ path
 91:           @locale_paths += [
 92:         "#{$1}/data/locale/%{locale}/LC_MESSAGES/%{name}.mo", 
 93:         "#{$1}/data/locale/%{locale}/%{name}.mo", 
 94:         "#{$1}/locale/%{locale}/%{name}.mo"]
 95:         end
 96:       end
 97:     end
 98:       end
 99:    
100:       @mofiles = Hash.new
101:       set_locale(locale)
102:     end

Public Instance methods

Gets the translated string.

  • msgid: the original message.
  • Returns: the translated string or nil if not found.

[Source]

     # File lib/gettext/textdomain.rb, line 117
117:     def gettext(msgid)
118:       return "" if msgid == "" or msgid.nil?
119:       return nil unless @current_mo
120:       if @current_mo[msgid] and (@current_mo[msgid].size > 0)
121:         @current_mo[msgid]
122:       elsif msgid.include?("\000")
123:         ret = nil
124:         msgid_single = msgid.split("\000")[0]
125:         @current_mo.each{|key, val| 
126:           if key =~ /^#{Regexp.quote(msgid_single)}\000/
127:               # Usually, this is not caused to make po-files from rgettext.
128:               warn %Q[Warning: n_("#{msgid_single}", "#{msgid.split("\000")[1]}") and n_("#{key.gsub(/\000/, '", "')}") are duplicated.] if $DEBUG
129:             ret = val
130:             break
131:           end
132:         }
133:         ret
134:       else
135:         ret = nil
136:         @current_mo.each{|key, val| 
137:           if key =~ /^#{Regexp.quote(msgid)}\000/
138:               ret = val.split("\000")[0]
139:             break
140:           end
141:         }
142:         ret
143:       end
144:     end

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.

[Source]

     # File lib/gettext/textdomain.rb, line 151
151:     def ngettext(msgid, msgid_plural, n)
152:       key = msgid + "\000" + msgid_plural
153:       msg = gettext(key)
154:       if ! msg
155:     nil  # do nothing.
156:       elsif msg == key
157:     msg = n == 1 ? msgid : msgid_plural
158:       elsif msg.include?("\000")
159:         ary = msg.split("\000")
160:         if @current_mo
161:           plural = eval(@current_mo.plural)
162:           if plural.kind_of?(Numeric)
163:             msg = ary[plural]
164:           else
165:             msg = plural ? ary[1] : ary[0]
166:           end
167:         else
168:           msg = n == 1 ? ary[0] : ary[1]
169:         end
170:       end
171:       msg
172:     end

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.

[Source]

     # File lib/gettext/textdomain.rb, line 198
198:     def same_property?(name, topdir, locale)
199:       @name == name and @topdir == topdir and @current_locale == locale
200:     end

Sets a new Locale::Object.

  • locale: a Locale::Object
  • reload: true if the mo-file is reloaded forcely
  • Returns: self

[Source]

     # File lib/gettext/textdomain.rb, line 108
108:     def set_locale(locale, reload = false)
109:       @current_locale = locale
110:       load_mo(reload)
111:       self
112:     end

[Validate]