Module: GetText::TextDomainManager

Extended by:
TextDomainManager
Included in:
TextDomainManager
Defined in:
lib/gettext/text_domain_manager.rb

Constant Summary collapse

@@text_domain_pool =
{}
@@text_domain_group_pool =
{}
@@output_charset =
nil
@@gettext_classes =
[]
@@singular_message_cache =
{}
@@plural_message_cache =
{}
@@cached =
! $DEBUG

Instance Method Summary collapse

Instance Method Details

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

bind text domain to the class.



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gettext/text_domain_manager.rb', line 67

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

  charset = options[:output_charset] || self.output_charset
  text_domain = create_or_find_text_domain(domainname,options[:path],charset)
  target_klass = ClassInfo.normalize_class(klass)
  create_or_find_text_domain_group(target_klass).add(text_domain)
  @@gettext_classes << target_klass unless @@gettext_classes.include? target_klass

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



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

def cached=(val)
  @@cached = val
  TextDomain.cached = val
end

#cached?Boolean

Return the cached value.

Returns:

  • (Boolean)


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

def cached?
  TextDomain.cached?
end

#clear_all_text_domainsObject

for testing.



203
204
205
206
207
208
# File 'lib/gettext/text_domain_manager.rb', line 203

def clear_all_text_domains
  @@text_domain_pool = {}
  @@text_domain_group_pool = {}
  @@gettext_classes = []
  clear_caches
end

#clear_cachesObject

for testing.



211
212
213
214
# File 'lib/gettext/text_domain_manager.rb', line 211

def clear_caches
  @@singular_message_cache = {}
  @@plural_message_cache = {}
end

#create_or_find_text_domain(name, path, charset) ⇒ Object

:nodoc:



223
224
225
226
227
228
# File 'lib/gettext/text_domain_manager.rb', line 223

def create_or_find_text_domain(name, path, charset)#:nodoc:
  text_domain = @@text_domain_pool[name]
  return text_domain if text_domain

  @@text_domain_pool[name] = TextDomain.new(name, path, charset)
end

#create_or_find_text_domain_group(klass) ⇒ Object

:nodoc:



216
217
218
219
220
221
# File 'lib/gettext/text_domain_manager.rb', line 216

def create_or_find_text_domain_group(klass) #:nodoc:
  group = @@text_domain_group_pool[klass]
  return group if group

  @@text_domain_group_pool[klass] = TextDomainGroup.new
end

#dump_all_text_domainsObject

for testing.



187
188
189
190
191
192
193
# File 'lib/gettext/text_domain_manager.rb', line 187

def dump_all_text_domains
  [
    @@text_domain_pool.dup,
    @@text_domain_group_pool.dup,
    @@gettext_classes.dup,
  ]
end

#each_text_domains(klass) ⇒ Object

:nodoc:



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gettext/text_domain_manager.rb', line 79

def each_text_domains(klass) #:nodoc:
  Locale.candidates.each do |lang|
    ClassInfo.related_classes(klass, @@gettext_classes).each do |target|
      if group = @@text_domain_group_pool[target]
        group.text_domains.each do |text_domain|
          yield text_domain, lang
        end
      end
    end
  end
end

#output_charsetObject

Gets the output charset.



54
55
56
# File 'lib/gettext/text_domain_manager.rb', line 54

def output_charset
  @@output_charset
end

#output_charset=(charset) ⇒ Object

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



59
60
61
62
63
64
# File 'lib/gettext/text_domain_manager.rb', line 59

def output_charset=(charset)
  @@output_charset = charset
  @@text_domain_pool.each do |key, text_domain|
    text_domain.output_charset = charset
  end
end

#restore_all_text_domains(dumped_all_text_domains) ⇒ Object

for testing.



196
197
198
199
200
# File 'lib/gettext/text_domain_manager.rb', line 196

def restore_all_text_domains(dumped_all_text_domains)
  @@text_domain_pool, @@text_domain_group_pool, @@gettext_classes =
    dumped_all_text_domains
  clear_caches
end

#text_domain_pool(domainname) ⇒ Object

Find text domain by name



34
35
36
# File 'lib/gettext/text_domain_manager.rb', line 34

def text_domain_pool(domainname)
  @@text_domain_pool[domainname]
end

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

This function is similar to the get_singular_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 "|".


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
# File 'lib/gettext/text_domain_manager.rb', line 141

def translate_plural_message(klass, arg1, arg2, arg3 = "|", arg4 = "|")
  klass = ClassInfo.normalize_class(klass)
  # parse arguments
  if arg1.kind_of?(Array)
    msgid = arg1[0]
    msgid_plural = arg1[1]
    n = arg2
    if arg3 and arg3.kind_of? Numeric
      raise ArgumentError, _("ngettext: 3rd parmeter is wrong: value = %{number}") % {:number => arg3}
    end
    div = arg3
  else
    msgid = arg1
    msgid_plural = arg2
    raise ArgumentError, _("ngettext: 3rd parameter should be a number, not nil.")  unless arg3
    n = arg3
    div = arg4
  end

  key = [Locale.current, klass, msgid, msgid_plural, div]
  msgs = @@plural_message_cache[key]
  unless (msgs and @@cached)
    # Find messages from related classes.
    msgs = nil
    each_text_domains(klass) do |text_domain, lang|
      msgs = text_domain.translate_plural_message(lang, msgid, msgid_plural)
      break if msgs
    end

    msgs = [[msgid, msgid_plural], TextDomain::DEFAULT_PLURAL_CALC] unless msgs

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

  # Return the singular or plural message.
  msgstrs = msgs[0]
  plural = msgs[1].call(n)
  return msgstrs[plural] if plural.kind_of?(Numeric)
  return plural ? msgstrs[1] : msgstrs[0]
end

#translate_singular_message(klass, msgid, div = nil) ⇒ 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".


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gettext/text_domain_manager.rb', line 98

def translate_singular_message(klass, msgid, div = nil)
  klass = ClassInfo.normalize_class(klass)
  key = [Locale.current, klass, msgid, div]
  msg = @@singular_message_cache[key]
  return msg if msg and @@cached
  # Find messages from related classes.
  each_text_domains(klass) do |text_domain, lang|
    msg = text_domain.translate_singular_message(lang, msgid)
    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
  @@singular_message_cache[key] = msg
end