Class: GetText::TextDomain

Inherits:
Object
  • Object
show all
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$/
DEFAULT_LOCALE_PATHS =

The default locale paths.

[
  "#{Config::CONFIG['datadir']}/locale/%{locale}/LC_MESSAGES/%{name}.mo",
  "#{Config::CONFIG['prefix']}/share/locale/%{locale}/LC_MESSAGES/%{name}.mo",
  "#{Config::CONFIG['prefix']}/local/share/locale/%{locale}/LC_MESSAGES/%{name}.mo"
].uniq
@@check_mo =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, topdir = nil, locale = nil) ⇒ TextDomain

Creates a new GetText::TextDomain.

  • name: the textdomain name.

  • topdir: the top directory of mo files or nil.

  • locale: the Locale::Object or nil.

  • Returns: a newly created GetText::TextDomain object.



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
# File 'lib/gettext/textdomain.rb', line 68

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_localeObject (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_moObject (readonly)

Returns the value of attribute current_mo.



30
31
32
# File 'lib/gettext/textdomain.rb', line 30

def current_mo
  @current_mo
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



59
60
61
# File 'lib/gettext/textdomain.rb', line 59

def self.add_default_locale_path(path)
  DEFAULT_LOCALE_PATHS << 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



45
46
47
# File 'lib/gettext/textdomain.rb', line 45

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.

Returns:

  • (Boolean)


38
39
40
# File 'lib/gettext/textdomain.rb', line 38

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.



113
114
115
116
117
118
119
120
# File 'lib/gettext/textdomain.rb', line 113

def gettext(msgid)
  return "" if msgid == "" or msgid.nil?
  if @current_mo and @current_mo[msgid] and (@current_mo[msgid].size > 0)
	@current_mo[msgid]
  else
    msgid
  end
end

#ngettext(msgid, msgid_plural, n) ⇒ Object

Gets the translated string.

  • msgid: the original message(single).

  • msgid: the original message(plural).

  • n: the number

  • Returns: the translated string.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/gettext/textdomain.rb', line 127

def ngettext(msgid, msgid_plural, n)
  key = msgid + "\000" + msgid_plural
  msg = gettext(key)
  if 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

#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.

Returns:

  • (Boolean)


153
154
155
# File 'lib/gettext/textdomain.rb', line 153

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



104
105
106
107
108
# File 'lib/gettext/textdomain.rb', line 104

def set_locale(locale, reload = false)
  @current_locale = locale
  load_mo(reload)
  self
end