Class: GetText::TextDomainManager

Inherits:
Object
  • Object
show all
Includes:
Locale::Util::Memoizable
Defined in:
lib/gettext/textdomain_manager.rb

Constant Summary collapse

@@cached =
! $DEBUG
@@textdomain_pool =
{}
@@textdomain_manager_pool =
{}
@@output_charset =
nil
@@gettext_classes =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTextDomainManager

Returns a new instance of TextDomainManager.



91
92
93
94
# File 'lib/gettext/textdomain_manager.rb', line 91

def initialize
  @textdomains = []
  @supported_language_tags = nil
end

Instance Attribute Details

#supported_language_tagsObject (readonly)

Returns the textdoman in the instance.



89
90
91
# File 'lib/gettext/textdomain_manager.rb', line 89

def supported_language_tags
  @supported_language_tags
end

#textdomainsObject (readonly)

Returns the textdoman in the instance.



89
90
91
# File 'lib/gettext/textdomain_manager.rb', line 89

def textdomains
  @textdomains
end

Class Method Details

.bind_to(klass, domainname, options = {}) ⇒ Object

bind textdomain to the class.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gettext/textdomain_manager.rb', line 62

def self.bind_to(klass, domainname, options = {})
  warn "Bind the domain '#{domainname}' to '#{klass}'. " if $DEBUG

  path = options[:path] if options[:path]
  charset = options[:output_charset] || self.output_charset

  # Create a new textdomain into the pool.
  textdomain = @@textdomain_pool[domainname]
  unless textdomain
    textdomain = TextDomain.new(domainname, path, charset)
  end
  @@textdomain_pool[domainname] = textdomain

  target_klass = ClassInfo.normalize_class(klass)

  get(target_klass).add(textdomain, options[:supported_language_tags])

  @@gettext_classes << target_klass unless @@gettext_classes.include? target_klass

  textdomain
end

.cached=(val) ⇒ Object

Set the value whether cache messages or not. true to cache messages, otherwise false.

Default is true. If $DEBUG is false, messages are not checked even if this value is true.



36
37
38
39
40
41
# File 'lib/gettext/textdomain_manager.rb', line 36

def self.cached=(val)
  @@cached = val
  @@textdomain_pool.each do |key, textdomain|
    textdomain.cached = val
  end
end

.cached?Boolean

Return the cached value.

Returns:

  • (Boolean)


44
45
46
# File 'lib/gettext/textdomain_manager.rb', line 44

def self.cached?
  @@cached
end

.clear_all_textdomainsObject

for testing.



212
213
214
215
216
# File 'lib/gettext/textdomain_manager.rb', line 212

def self.clear_all_textdomains
  @@textdomain_pool = {}
  @@textdomain_manager_pool = {}
  @@gettext_classes = []
end

.get(obj) ⇒ Object

:nodoc:



21
22
23
24
25
26
27
28
29
# File 'lib/gettext/textdomain_manager.rb', line 21

def self.get(obj) #:nodoc:
  klass = ClassInfo.normalize_class(obj)
  ret = @@textdomain_manager_pool[klass]
  unless ret
    ret = TextDomainManager.new
    @@textdomain_manager_pool[klass] = ret
  end
  ret
end

.output_charsetObject

Gets the output charset.



49
50
51
# File 'lib/gettext/textdomain_manager.rb', line 49

def self.output_charset
  @@output_charset 
end

.output_charset=(charset) ⇒ Object

Sets the output charset.The program can have a output charset.



54
55
56
57
58
59
# File 'lib/gettext/textdomain_manager.rb', line 54

def self.output_charset=(charset)
  @@output_charset = charset
  @@textdomain_pool.each do |key, textdomain|
    textdomain.charset = charset
  end
end

.textdomain_pool(domainname) ⇒ Object

Gets all textdomains.



17
18
19
# File 'lib/gettext/textdomain_manager.rb', line 17

def self.textdomain_pool(domainname)
  @@textdomain_pool[domainname]
end

Instance Method Details

#add(textdomain, supported_language_tags) ⇒ Object



96
97
98
99
# File 'lib/gettext/textdomain_manager.rb', line 96

def add(textdomain, supported_language_tags)
  @textdomains.insert(0, textdomain) unless @textdomains.include? textdomain
  @supported_language_tags = supported_language_tags if supported_language_tags
end

#translate_plural_message(klass, arg1, arg2, arg3 = "|", arg4 = "|") ⇒ Object

This function is similar to the get_singluar_message function as it finds the message catalogs in the same way. But it takes two extra arguments for plural form. The msgid parameter must contain the singular form of the string to be converted. It is also used as the key for the search in the catalog. The msgid_plural parameter is the plural form. The parameter n is used to determine the plural form. If no message catalog is found msgid1 is returned if n == 1, otherwise msgid2. And if msgid includes “div”, it returns a last part of msgid separeted “div”.

  • msgid: the singular form with “div”. (e.g. “Special|An apple”, “An apple”)

  • msgid_plural: the plural form. (e.g. “%num Apples”)

  • n: a number used to determine the plural form.

  • div: the separator. Default is “|”.

  • Returns: the localized text which key is msgid_plural if n is plural(follow plural-rule) or msgid. “plural-rule” is defined in po-file.

or

  • msgid, msgid_plural

    : msgid and msgid_plural an Array

  • n: a number used to determine the plural form.

  • div: the separator. Default is “|”.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/gettext/textdomain_manager.rb', line 159

def translate_plural_message(klass, arg1, arg2, arg3 = "|", arg4 = "|")
  lang = Locale.candidates(:supported_language_tags => @supported_language_tags,
                           :type => :posix)[0]
  
  if arg1.kind_of?(Array)
    msgid = arg1[0]
    msgid_plural = arg1[1]
    n = arg2
    if arg3 and arg3.kind_of? Numeric
      raise ArgumentError, _("3rd parmeter is wrong: value = %{number}") % {:number => arg3}
    end
    div = arg3
  else
    msgid = arg1
    msgid_plural = arg2
    n = arg3
    div = arg4
  end

  msgs = nil
  # Find messages from related classes.
  ClassInfo.related_classes(klass, @@gettext_classes).each do |target|
    msgs = nil
    self.class.get(target).textdomains.each do |textdomain|
      msgs = textdomain.translate_plural_message(lang, msgid, msgid_plural)
      break if msgs
    end
    break if msgs
  end
  
  # If not found, return msgid.
  unless msgs
    msgs = [[msgid, msgid_plural], "n != 1"]
  end

  msgstrs = msgs[0]
  if div and msgstrs[0] == msgid
    if index = msgstrs[0].rindex(div)
      msgstrs[0] = msgstrs[0][(index + 1)..-1]
    end
  end

  # Return the singular or plural message.
  plural = eval(msgs[1])
  if plural.kind_of?(Numeric)
    ret = msgstrs[plural]
  else
    ret = plural ? msgstrs[1] : msgstrs[0]
  end
  ret
end

#translate_singluar_message(klass, msgid, div = '|') ⇒ Object

Translates msgid, but if there are no localized text, it returns a last part of msgid separeted “div” or whole of the msgid with no “div”.

  • msgid: the message id.

  • div: separator or nil.

  • Returns: the localized text by msgid. If there are no localized text, it returns a last part of msgid separeted “div”.



108
109
110
111
112
# File 'lib/gettext/textdomain_manager.rb', line 108

def translate_singluar_message(klass, msgid, div = '|')
  lang = Locale.candidates(:supported_language_tags => @supported_language_tags, 
                           :type => :posix)[0]
  translate_singluar_message_to(lang, klass, msgid, div)
end

#translate_singluar_message_to(lang, klass, msgid, div = '|') ⇒ Object

:nodoc:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/gettext/textdomain_manager.rb', line 114

def translate_singluar_message_to(lang, klass, msgid, div = '|') #:nodoc:
  msg = nil
  # Find messages from related classes.
  ClassInfo.related_classes(klass, @@gettext_classes).each do |target|
    msg = nil
    self.class.get(target).textdomains.each do |textdomain|
      msg = textdomain.translate_singluar_message(lang, msgid)
      break if msg
    end
    break if msg
  end
  
  # If not found, return msgid.
  msg ||= msgid
  if div and msg == msgid
    if index = msg.rindex(div)
      msg = msg[(index + 1)..-1]
    end
  end
  msg
end