Method: GetText::TextDomain#translate_singular_message

Defined in:
lib/gettext/text_domain.rb

#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
    msgid_prefix_re = /^#{Regexp.quote(msgid)}\000/
    mo.each do |key, val|
      if msgid_prefix_re =~ key
        ret = val.split("\000")[0]
        break
      end
    end
  end
  ret
end