Class: GetText::Tools::MsgMerge::PoData

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePoData

Returns a new instance of PoData.



36
37
38
39
40
# File 'lib/gettext/tools/msgmerge.rb', line 36

def initialize
  @msgid2msgstr = {}
  @msgid2comment = {}
  @msgids = []
end

Instance Attribute Details

#msgidsObject (readonly)

Returns the value of attribute msgids.



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

def msgids
  @msgids
end

Instance Method Details

#[](msgid) ⇒ Object



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

def [](msgid)
  @msgid2msgstr[msgid]
end

#[]=(msgid, msgstr) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/gettext/tools/msgmerge.rb', line 58

def []=(msgid, msgstr)
  # Retain the order
  unless @msgid2msgstr.has_key?(msgid)
    @msgids << msgid
  end

  @msgid2msgstr[msgid] = msgstr
end

#__conv(str) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/gettext/tools/msgmerge.rb', line 176

def __conv(str)
  s = ""

  if str.count("\n") > 1
    s << '""' << "\n"
    str.each_line do |line|
      s << '"' << escape(line) << '"' << "\n"
    end
  else
    s << '"' << escape(str) << '"'
  end

  s.rstrip
end

#comment(msgid) ⇒ Object



50
51
52
# File 'lib/gettext/tools/msgmerge.rb', line 50

def comment(msgid)
  @msgid2comment[msgid]
end

#each_msgidObject



67
68
69
70
71
72
73
74
75
# File 'lib/gettext/tools/msgmerge.rb', line 67

def each_msgid
  msgids = @msgids.delete_if do |msgid|
    msgid.kind_of?(Symbol) or msgid.empty?
  end

  msgids.each do |msgid|
    yield(msgid)
  end
end

#escape(string) ⇒ Object



191
192
193
# File 'lib/gettext/tools/msgmerge.rb', line 191

def escape(string)
  PoMessage.escape(string)
end

#generate_poObject



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

def generate_po
  str = ""
  str << generate_po_header
  str << "\n"

  po_entries = []
  self.each_msgid do |id|
    po_entries << self.generate_po_entry(id)
  end

  unless @msgid2comment[:last].nil?
    po_entries << "#{@msgid2comment[:last]}\n"
  end

  str << po_entries.join("\n")
  str
end

#generate_po_entry(msgid) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
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
# File 'lib/gettext/tools/msgmerge.rb', line 130

def generate_po_entry(msgid)
  str = ""
  str << @msgid2comment[msgid]
  if str[-1] != "\n"[0]
    str << "\n"
  end

  id = msgid.gsub(/\r/, "")
  msgstr = @msgid2msgstr[msgid]
  if msgstr.nil?
    msgstr = ""
  else
    msgstr = msgstr.gsub(/\r/, "")
  end

  if id.include?("\004")
    ids = id.split(/\004/)
    context = ids[0]
    id      = ids[1]
    str << "msgctxt "  << __conv(context) << "\n"
  end

  if id.include?("\000")
    ids = id.split(/\000/)
    str << "msgid " << __conv(ids[0]) << "\n"
    ids[1..-1].each do |single_id|
      str << "msgid_plural " << __conv(single_id) << "\n"
    end

    if msgstr.empty?
      nplurals.times do |id_count|
        str << "msgstr[#{id_count}] " << '""' << "\n"
      end
    else
      msgstr.split("\000", -1).each_with_index do |m, n|
        str << "msgstr[#{n}] " << __conv(m) << "\n"
      end
    end
  else
    str << "msgid "  << __conv(id) << "\n"
    str << "msgstr " << __conv(msgstr) << "\n"
  end

  str
end

#generate_po_headerObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/gettext/tools/msgmerge.rb', line 116

def generate_po_header
  str = ""

  str << @msgid2comment[""].strip << "\n"
  str << 'msgid ""'  << "\n"
  str << 'msgstr ""' << "\n"
  msgstr = @msgid2msgstr[""].gsub(/"/, '\"').gsub(/\r/, "")
  msgstr = msgstr.gsub(/^(.*)$/, '"\1\n"')
  str << msgstr.chomp
  str << "\n"

  str
end

#msgid?(msgid) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/gettext/tools/msgmerge.rb', line 77

def msgid?(msgid)
  return false if msgid.kind_of?(Symbol)
  return true if msgid.empty?
  @msgid2msgstr.has_key?(msgid)
end

#msgstr(msgid) ⇒ Object



46
47
48
# File 'lib/gettext/tools/msgmerge.rb', line 46

def msgstr(msgid)
  @msgid2msgstr[msgid]
end

#npluralsObject



88
89
90
91
92
93
94
95
96
# File 'lib/gettext/tools/msgmerge.rb', line 88

def nplurals
  return 0 if @msgid2msgstr[""].nil?

  if /\s*nplurals\s*=\s*(\d+)/ =~ @msgid2msgstr[""]
    return $1.to_i
  else
    return 0
  end
end

#search_msgid_fuzzy(msgid, used_msgids) ⇒ Object

Is it necessary to implement this method?



84
85
86
# File 'lib/gettext/tools/msgmerge.rb', line 84

def search_msgid_fuzzy(msgid, used_msgids)
  nil
end

#set_comment(msgid_or_sym, comment) ⇒ Object



42
43
44
# File 'lib/gettext/tools/msgmerge.rb', line 42

def set_comment(msgid_or_sym, comment)
  @msgid2comment[msgid_or_sym] = comment
end