Skip to content

Commit a7cd96f

Browse files
committed
Create plural string msgstr instance variables in Entry class
1 parent 629073d commit a7cd96f

File tree

5 files changed

+25
-24
lines changed

5 files changed

+25
-24
lines changed

lib/poparser/constants.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ module PoParser
1717
msgid: 'msgid',
1818
msgid_plural: 'msgid_plural',
1919
msgstr: 'msgstr',
20-
}.freeze
20+
}
21+
(0..9).to_a.each { |index| ENTRIES_LABELS["msgstr_#{index}".to_sym] = "msgstr[#{index}]" }
22+
ENTRIES_LABELS.freeze
2123

2224
LABELS = COMMENTS_LABELS.merge(ENTRIES_LABELS).keys
2325

lib/poparser/entry.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def to_h
8484
object = instance_variable_get(label)
8585
next if object.nil?
8686

87-
# If it's a plural msgstr
87+
# If it's a multiline message/comment
8888
if object.value.is_a?(Array)
8989
hash[object.type] = object.value.compact
9090
else
@@ -120,9 +120,8 @@ def set_instance_variable(name, value)
120120
elsif ENTRIES_LABELS.include? name
121121
instance_variable_set "@#{name}".to_sym, Message.new(name, value)
122122
elsif /^msgstr\[[0-9]\]/.match?(name.to_s)
123-
# If it's a plural msgstr
124-
@msgstr ||= []
125-
@msgstr << Message.new(name, value)
123+
# If it's a plural msgstr, change instance variable name to @msgstr_n as @msgstr[n] is not a valid variable name
124+
instance_variable_set "@msgstr_#{plural_form(name)}".to_sym, Message.new(name, value)
126125
end
127126
end
128127

@@ -142,6 +141,10 @@ def define_writer_method(type, object)
142141
klass = instance_variable_get "@#{type}".to_sym
143142
klass.type = type
144143
klass.value = val
144+
elsif type.match(/^msgstr_\d/)
145+
plural_form = type.to_s.scan(/^msgstr_(\d)/).last.first.to_i
146+
object_type = "msgstr[#{plural_form}]".to_sym
147+
instance_variable_set "@#{type}".to_sym, object.new(object_type, val)
145148
else
146149
instance_variable_set "@#{type}".to_sym, object.new(type, val)
147150
end
@@ -199,5 +202,9 @@ def define_aliases
199202
self.class.send(:alias_method, :refrence, :reference)
200203
self.class.send(:alias_method, :refrence=, :reference=)
201204
end
205+
206+
def plural_form(name)
207+
name.to_s.scan(/^msgstr\[([0-9])\]/).last.first.to_i
208+
end
202209
end
203210
end

lib/poparser/message.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ def to_s(with_label = false)
2020

2121
if @value.is_a? Array
2222
remove_empty_line
23-
# special case for plural strings
24-
return msgstr_plural_to_s if label == 'msgstr'
25-
2623
# multiline messages should be started with an empty line
2724
lines = ["#{label} \"\"\n"]
2825
@value.each do |str|
@@ -50,14 +47,6 @@ def remove_empty_line
5047
end
5148
end
5249

53-
def msgstr_plural_to_s
54-
lines = []
55-
@value.each_with_index do |str, index|
56-
lines << "msgstr[#{index}] \"#{str}\"\n"
57-
end
58-
lines.join
59-
end
60-
6150
def label
6251
if /msgstr\[[0-9]\]/.match?(@type.to_s)
6352
@type

spec/poparser/entry_spec.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@
3737

3838
it 'should show a hash presentation of a plural string entry' do
3939
@entry = PoParser::Entry.new
40-
@entry.msgid_plural = 'word'
41-
@entry.msgstr = %w[mot mots]
40+
@entry.msgid_plural = 'right word'
41+
@entry.msgstr_0 = %w[mot juste]
42+
@entry.msgstr_1 = %w[mots justes]
4243
@entry.translator_comment = ['comment', 'second line of comments']
44+
4345
result = {
4446
:translator_comment => ['comment', 'second line of comments'],
45-
:msgid_plural => 'word',
46-
:msgstr => %w[mot mots]
47+
:msgid_plural => 'right word',
48+
'msgstr[0]': %w[mot juste],
49+
'msgstr[1]': %w[mots justes]
4750
}
4851
expect(@entry.to_h).to eq(result)
4952
end
@@ -103,7 +106,7 @@
103106
@entry.flag = 'fuzzy'
104107
@entry.msgid = ['first line', 'second line']
105108
@entry.msgstr = ['first line', 'second line']
106-
result = "#, fuzzy\nmsgid \"\"\n\"first line\"\n\"second line\"\nmsgstr[0] \"first line\"\nmsgstr[1] \"second line\"\n"
109+
result = "#, fuzzy\nmsgid \"\"\n\"first line\"\n\"second line\"\nmsgstr \"\"\n\"first line\"\n\"second line\"\n"
107110
expect(@entry.to_s).to eq(result)
108111
end
109112
end

spec/poparser/po_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
'msgstr[1]': 'phrases',
2929
}
3030
@po << {
31-
msgid_plural: 'word',
32-
msgstr: %w[word words]
31+
msgid: 'multiline word',
32+
msgstr: %w[line1 line2]
3333
}
3434
entry_1 = "# comment\n# another comment line\n#: reference comment\nmsgid \"untranslated\"\nmsgstr \"translated string\"\n"
3535
entry_2 = "msgid_plural \"phrase\"\nmsgstr[0] \"phrase\"\nmsgstr[1] \"phrases\"\n"
36-
entry_3 = "msgid_plural \"word\"\nmsgstr[0] \"word\"\nmsgstr[1] \"words\"\n"
36+
entry_3 = "msgid \"multiline word\"\nmsgstr \"\"\n\"line1\"\n\"line2\"\n"
3737

3838
expect(@po.to_s).to eq(entry_1 + "\n" + entry_2 + "\n" + entry_3)
3939
end

0 commit comments

Comments
 (0)