Class: GetText::TextDomain

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext/runtime/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

DEFAULT_PLURAL_CALC =
Proc.new{|n| n != 1}
DEFAULT_SINGLE_CALC =
Proc.new{|n| 0}
@@cached =
! $DEBUG
# Cache the mo-file or not.
# Default is true. If $DEBUG is set then false.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new GetText::TextDomain.

  • name: the textdomain name.

  • topdir: the locale path (“%topdir/%lang/LC_MESSAGES/%#name.mo”) or nil.

  • output_charset: output charset.

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



57
58
59
60
61
62
# File 'lib/gettext/runtime/textdomain.rb', line 57

def initialize(name, topdir = nil, output_charset = nil)
  @name, @output_charset = name, output_charset

  @locale_path = LocalePath.new(@name, topdir)
  @mofiles = {}
end

Instance Attribute Details

#mofilesObject (readonly)

Returns the value of attribute mofiles.



27
28
29
# File 'lib/gettext/runtime/textdomain.rb', line 27

def mofiles
  @mofiles
end

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/gettext/runtime/textdomain.rb', line 28

def name
  @name
end

#output_charsetObject

Returns the value of attribute output_charset.



26
27
28
# File 'lib/gettext/runtime/textdomain.rb', line 26

def output_charset
  @output_charset
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/%lang/LC_MESSAGES/%#name.mo” (‘locale’ => “ja_JP”, ‘name’ => “textdomain”)

  • Returns: the new DEFAULT_LOCALE_PATHS



47
48
49
50
# File 'lib/gettext/runtime/textdomain.rb', line 47

def self.add_default_locale_path(path)
  warn "Deprecated. Use GetText::LocalePath.add_default_rule instead."
  LocalePath.add_default_rule(path)
end

.cached=(val) ⇒ Object

Set to cache the mo-file or not.

  • val: true if cached, otherwise false.



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

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

.cached?Boolean

Cache the mo-file or not. Default is true. If $DEBUG is set then false.

Returns:

  • (Boolean)


33
34
35
# File 'lib/gettext/runtime/textdomain.rb', line 33

def self.cached?
  @@cached
end

Instance Method Details

#clearObject

Clear cached mofiles.



139
140
141
# File 'lib/gettext/runtime/textdomain.rb', line 139

def clear
  @mofiles = {}
end

#translate_plural_message(lang, msgid, msgid_plural) ⇒ Object

Translates the translated string.

  • lang: Locale::Tag::Simple’s subclass.

  • msgid: the original message.

  • msgid_plural: the original message(plural).

  • Returns: the translated string as an Array ([[msgstr1, msgstr2, …], cond]) or nil.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gettext/runtime/textdomain.rb', line 121

def translate_plural_message(lang, msgid, msgid_plural)   #:nodoc:
  key = msgid + "\000" + msgid_plural
  msg = translate_singluar_message(lang, key)
  ret = nil
  if ! msg
    ret = nil
  elsif msg.include?("\000")
    # [[msgstr[0], msgstr[1], msgstr[2],...], cond]
    mofile = @mofiles[lang.to_posix.to_s]
    cond = (mofile and mofile != :empty) ? mofile.plural_as_proc : DEFAULT_PLURAL_CALC
    ret = [msg.split("\000"), cond]
  else
    ret = [[msg], DEFAULT_SINGLE_CALC]
  end
  ret
end

#translate_singluar_message(lang, msgid) ⇒ Object

Translates the translated string.

  • lang: Locale::Tag::Simple’s subclass.

  • msgid: the original message.

  • Returns: the translated string or nil.



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
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gettext/runtime/textdomain.rb', line 68

def translate_singluar_message(lang, msgid)
  return "" if msgid == "" or msgid.nil?

  lang_key = lang.to_s

  mofile = nil
  if self.class.cached?
    mofile = @mofiles[lang_key]
  end
  unless mofile
    mofile = load_mo(lang)
  end
 
  if (! mofile) or (mofile ==:empty)
    return nil
  end

  msgstr = mofile[msgid]
  if msgstr and (msgstr.size > 0)
    msgstr
  elsif msgid.include?("\000")
    # Check "aaa\000bbb" and show warning but return the singluar part.
    ret = nil
    msgid_single = msgid.split("\000")[0]
    mofile.each{|key, val| 
      if key =~ /^#{Regexp.quote(msgid_single)}\000/
        # Usually, this is not caused to make po-files from rgettext.
        warn %Q[Warning: n_("#{msgid_single}", "#{msgid.split("\000")[1]}") and n_("#{key.gsub(/\000/, '", "')}") are duplicated.]
        ret = val
        break
      end
    }
    ret
  else
    ret = nil
    mofile.each{|key, val| 
      if key =~ /^#{Regexp.quote(msgid)}\000/
        ret = val.split("\000")[0]
        break
      end
    }
    ret
  end
end