Class: GetText::TextDomain

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext/text_domain.rb

Overview

GetText::TextDomain class manages mo-files of a text domain.

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

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 text domain name.
  • topdir: the locale path ("%topdir/%lang/LC_MESSAGES/%#name.mo") or nil.
  • output_charset: output charset.
  • Returns: a newly created GetText::TextDomain object.

49
50
51
52
53
54
# File 'lib/gettext/text_domain.rb', line 49

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


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

def mofiles
  @mofiles
end

#nameObject (readonly)

Returns the value of attribute name


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

def name
  @name
end

#output_charsetObject

Returns the value of attribute output_charset


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

def output_charset
  @output_charset
end

Class Method Details

.cached=(val) ⇒ Object

Set to cache the mo-file or not.

  • val: true if cached, otherwise false.

40
41
42
# File 'lib/gettext/text_domain.rb', line 40

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)

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

def self.cached?
  @@cached
end

Instance Method Details

#clearObject

Clear cached mofiles.


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

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.

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/gettext/text_domain.rb', line 117

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

#translate_singular_message(lang, msgid) ⇒ Object

Translates the translated string.

  • lang: Locale::Tag::Simple's subclass.
  • msgid: the original message.
  • Returns: the translated string or nil.

60
61
62
63
64
65
66
67
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
# File 'lib/gettext/text_domain.rb', line 60

def translate_singular_message(lang, msgid)
  return "" if msgid.nil?

  lang_key = lang.to_s

  mo = nil
  if self.class.cached?
    mo = @mofiles[lang_key]
  end
  unless mo
    mo = load_mo(lang)
  end

  if (! mo) or (mo ==:empty)
    return nil
  end

  return mo[msgid] if mo.has_key?(msgid)

  ret = nil
  if msgid.include?("\000")
    # Check "aaa\000bbb" and show warning but return the singular part.
    msgid_single = msgid.split("\000")[0]
    msgid_single_prefix_re = /^#{Regexp.quote(msgid_single)}\000/
    mo.each do |key, val|
      if msgid_single_prefix_re =~ key
        # Usually, this is not caused to make po-files from rgettext.
        separated_msgid = msgid.gsub(/\000/, '", "')
        duplicated_msgid = key.gsub(/\000/, '", "')
        warn("Warning: " +
              "n_(\"#{separated_msgid}\") and " +
              "n_(\"#{duplicated_msgid}\") " +
              "are duplicated.")
        ret = val
        break
      end
    end
  else
    plural_msgid_prefix = "#{msgid}\000"
    mo.each do |key, val|
      next unless Encoding.compatible?(key, plural_msgid_prefix)
      next unless key.start_with?(plural_msgid_prefix)
      ret = val.split("\000")[0]
      break
    end
  end
  ret
end