Module: GetText

Extended by:
GetText
Included in:
GetText, GladeParser, Tools::MsgFmt, Tools::MsgInit, Tools::Task, Tools::XGetText
Defined in:
lib/gettext/version.rb,
lib/gettext.rb,
lib/gettext/mo.rb,
lib/gettext/po.rb,
lib/gettext/cgi.rb,
lib/gettext/po_entry.rb,
lib/gettext/po_format.rb,
lib/gettext/po_parser.rb,
lib/gettext/class_info.rb,
lib/gettext/tools/task.rb,
lib/gettext/locale_path.rb,
lib/gettext/text_domain.rb,
lib/gettext/tools/msgcat.rb,
lib/gettext/tools/msgfmt.rb,
lib/gettext/tools/msginit.rb,
lib/gettext/tools/msgmerge.rb,
lib/gettext/tools/xgettext.rb,
lib/gettext/tools/parser/erb.rb,
lib/gettext/text_domain_group.rb,
lib/gettext/tools/parser/ruby.rb,
lib/gettext/tools/parser/glade.rb,
lib/gettext/text_domain_manager.rb

Overview

gettext/text_domain_group - GetText::TextDomainGroup class

Copyright (C) 2009 Masao Mutoh

You may redistribute it and/or modify it under the same license terms as Ruby or LGPL.

Defined Under Namespace

Modules: ClassInfo, POEntryForRubyParser, POFormat, TextDomainManager, Tools Classes: ErbParser, GladeParser, LocalePath, MO, NoboundTextDomainError, PO, POEntry, POParser, ParseError, RubyLexX, RubyParser, TextDomain, TextDomainGroup

Constant Summary

VERSION =
"3.2.3"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object

:nodoc:



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

def self.included(mod)  #:nodoc:
  mod.extend self
end

Instance Method Details

#bindtextdomain(domainname, *options) ⇒ Object

bindtextdomain(domainname, options = {})

Bind a text domain(%path/%#locale/LC_MESSAGES/%domainname.mo) to your program. Normally, the texdomain scope becomes the class/module(and parent classes/included modules).

  • domainname: the text domain name.
  • options: options as an Hash.
    • :path - the path to the mo-files. When the value is nil, it will search default paths such as /usr/share/locale, /usr/local/share/locale)
    • :output_charset - The output charset. Same with GetText.set_output_charset. Usually, L10n library doesn't use this option. Application may use this once.
  • Returns: the GetText::TextDomainManager.


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

def bindtextdomain(domainname, *options)
  bindtextdomain_to(self, domainname, *options)
end

#bindtextdomain_to(klass, domainname, *options) ⇒ Object

Includes GetText module and bind a text domain to a class.

  • klass: the target ruby class.
  • domainname: the text domain name.
  • options: options as an Hash. See GetText.bindtextdomain.


61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gettext.rb', line 61

def bindtextdomain_to(klass, domainname, *options)
  if options[0].kind_of? Hash
    opts = options[0]
  else
    # for backward compatibility.
    opts = {}
    opts[:path] = options[0] if options[0]
    opts[:output_charset] = options[2] if options[2]
  end
  unless (klass.kind_of? GetText or klass.include? GetText)
    klass.__send__(:include, GetText)
  end
  TextDomainManager.bind_to(klass, domainname, opts)
end

#cgiObject

Gets the CGI object. If it is nil, returns new CGI object. This methods is appeared when requiring "gettext/cgi".

  • Returns: the CGI object


36
37
38
# File 'lib/gettext/cgi.rb', line 36

def cgi
  Locale.cgi
end

#cgi=(cgi_) ⇒ Object

Same as GetText.set_cgi. This methods is appeared when requiring "gettext/cgi".

  • cgi_: CGI object
  • Returns: cgi_


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

def cgi=(cgi_)
  set_cgi(cgi_)
  cgi_
end

#gettext(msgid) ⇒ Object Also known as: _

call-seq: gettext(msgid) _(msgid)

Translates msgid and return the message. This doesn't make a copy of the message.

You need to use String#dup if you want to modify the return value with destructive functions.

(e.g.1) _("Hello ").dup << "world"

But e.g.1 should be rewrite to:

(e.g.2) _("Hello %val") % => "world"

Because the translator may want to change the position of "world".

  • msgid: the message id.
  • Returns: localized text by msgid. If there are not binded mo-file, it will return msgid.


117
118
119
# File 'lib/gettext.rb', line 117

def gettext(msgid)
  TextDomainManager.translate_singular_message(self, msgid)
end

#localeObject



277
278
279
# File 'lib/gettext.rb', line 277

def locale
  Locale.current[0]
end

#N_(msgid) ⇒ Object

makes dynamic translation messages readable for the gettext parser. (fruit) cannot be understood by the gettext parser. To help the parser find all your translations, you can add fruit = N("Apple") which does not translate, but tells the parser: "Apple" needs translation.

  • msgid: the message id.
  • Returns: msgid.


234
235
236
# File 'lib/gettext.rb', line 234

def N_(msgid)
  msgid
end

#ngettext(msgid, msgid_plural, n = nil) ⇒ Object Also known as: n_

call-seq: ngettext(msgid, msgid_plural, n) ngettext(msgids, n) # msgids = [msgid, msgid_plural] n_(msgid, msgid_plural, n) n_(msgids, n) # msgids = [msgid, msgid_plural]

The ngettext is similar to the gettext function as it finds the message catalogs in the same way. But it takes two extra arguments for plural form.

  • msgid: the singular form.
  • msgid_plural: the plural form.
  • n: a number used to determine the plural form.
  • 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.


169
170
171
# File 'lib/gettext.rb', line 169

def ngettext(msgid, msgid_plural, n = nil)
  TextDomainManager.translate_plural_message(self, msgid, msgid_plural, n)
end

#Nn_(msgid, msgid_plural) ⇒ Object

This is same function as N_ but for ngettext.

  • msgid: the message id.
  • msgid_plural: the plural message id.
  • Returns: msgid.


242
243
244
# File 'lib/gettext.rb', line 242

def Nn_(msgid, msgid_plural)
  [msgid, msgid_plural]
end

#npgettext(msgctxt, msgids, arg2 = nil, arg3 = nil) ⇒ Object Also known as: np_

call-seq: npgettext(msgctxt, msgid, msgid_plural, n) npgettext(msgctxt, msgids, n) # msgids = [msgid, msgid_plural] np_(msgctxt, msgid, msgid_plural, n) np_(msgctxt, msgids, n) # msgids = [msgid, msgid_plural]

The npgettext is similar to the nsgettext function. e.g.) np_("Special", "An apple", "%num Apples", num) == ns_("Special|An apple", "%num Apples", num)

  • msgctxt: the message context.
  • msgid: the singular form.
  • msgid_plural: the plural form.
  • n: a number used to determine the plural form.
  • 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.


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/gettext.rb', line 206

def npgettext(msgctxt, msgids, arg2 = nil, arg3 = nil)
  if msgids.kind_of?(Array)
    msgid = msgids[0]
    msgid_ctxt = "#{msgctxt}\004#{msgid}"
    msgid_plural = msgids[1]
    opt1 = arg2
    opt2 = arg3
  else
    msgid = msgids
    msgid_ctxt = "#{msgctxt}\004#{msgid}"
    msgid_plural = arg2
    opt1 = arg3
    opt2 = nil
  end

  msgstr = TextDomainManager.translate_plural_message(self, msgid_ctxt, msgid_plural, opt1, opt2)
  if msgstr == msgid_ctxt
    msgid
  else
    msgstr
  end
end

#nsgettext(msgid, msgid_plural, n = "|", seperator = "|") ⇒ Object Also known as: ns_

call-seq: nsgettext(msgid, msgid_plural, n, div = "|") nsgettext(msgids, n, div = "|") # msgids = [msgid, msgid_plural] ns_(msgid, msgid_plural, n, div = "|") ns_(msgids, n, div = "|") # msgids = [msgid, msgid_plural]

The nsgettext is similar to the ngettext. But if there are no localized text, it returns a last part of msgid separeted "div".

  • msgid: the singular form with "div". (e.g. "Special|An apple")
  • msgid_plural: the plural form. (e.g. "%num Apples")
  • n: a number used to determine the plural form.
  • 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.


188
189
190
# File 'lib/gettext.rb', line 188

def nsgettext(msgid, msgid_plural, n="|", seperator = "|")
  TextDomainManager.translate_plural_message(self, msgid, msgid_plural, n, seperator)
end

#output_charsetObject

Gets the current output_charset which is set using GetText.set_output_charset.

  • Returns: output_charset.


257
258
259
# File 'lib/gettext.rb', line 257

def output_charset
  TextDomainManager.output_charset
end

#pgettext(msgctxt, msgid) ⇒ Object Also known as: p_

call-seq: pgettext(msgctxt, msgid) p_(msgctxt, msgid)

Translates msgid with msgctxt. This methods is similer with s_(). e.g.) p_("File", "New") == s_("File|New") p_("File", "Open") == s_("File|Open")



151
152
153
# File 'lib/gettext.rb', line 151

def pgettext(msgctxt, msgid)
  TextDomainManager.translate_singular_message(self, "#{msgctxt}\004#{msgid}", "\004")
end

#set_cgi(cgi_) ⇒ Object

Sets a CGI object. This methods is appeared when requiring "gettext/cgi".

  • cgi_: CGI object
  • Returns: self


22
23
24
# File 'lib/gettext/cgi.rb', line 22

def set_cgi(cgi_)
  Locale.set_cgi(cgi_)
end

#set_current_locale(lang) ⇒ Object Also known as: current_locale=

Set the locale to the current thread. Note that if #set_locale is set, this value is ignored. If you need, set_locale(nil); set_current_locale(lang)



273
274
275
# File 'lib/gettext.rb', line 273

def set_current_locale(lang)
  Locale.current = lang
end

#set_locale(lang) ⇒ Object Also known as: locale=

Set the locale. This value forces the locale whole the programs. This method calls Locale.set_app_language_tags, Locale.default, Locale.current. Use Locale methods if you need to handle locales more flexible.



264
265
266
267
268
# File 'lib/gettext.rb', line 264

def set_locale(lang)
  Locale.set_app_language_tags(lang)
  Locale.default = lang
  Locale.current = lang
end

#set_output_charset(charset) ⇒ Object Also known as: output_charset=

Sets charset(String) such as "euc-jp", "sjis", "CP932", "utf-8", ... You shouldn't use this in your own Libraries.

  • charset: an output_charset
  • Returns: self


250
251
252
253
# File 'lib/gettext.rb', line 250

def set_output_charset(charset)
  TextDomainManager.output_charset = charset
  self
end

#sgettext(msgid, seperator = "|") ⇒ Object Also known as: s_

call-seq: sgettext(msgid, div = '|') s_(msgid, div = '|')

Translates msgid, but if there are no localized text, it returns a last part of msgid separeted "div".



134
135
136
# File 'lib/gettext.rb', line 134

def sgettext(msgid, seperator = "|")
  TextDomainManager.translate_singular_message(self, msgid, seperator)
end

#textdomain(domainname) ⇒ Object

Binds a existed text domain to your program. This is the same function with GetText.bindtextdomain but simpler(and faster) than bindtextdomain. Note that you need to call GetText.bindtextdomain first. If the domainname hasn't bound yet, raises GetText::NoboundTextDomainError.

  • domainname: a text domain name.
  • Returns: the GetText::TextDomainManager.


82
83
84
# File 'lib/gettext.rb', line 82

def textdomain(domainname) #:nodoc:
  textdomain_to(self, domainname)
end

#textdomain_to(klass, domainname) ⇒ Object

Includes GetText module and bind an exsited text domain to a class. See text domain for more detail.

  • klass: the target ruby class.
  • domainname: the text domain name.


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

def textdomain_to(klass, domainname)  #:nodoc:
  domain = TextDomainManager.text_domain_pool(domainname)
  raise NoboundTextDomainError.new(domainname) unless domain
  bindtextdomain_to(klass, domainname)
end