Skip to content

Commit 0a90086

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

5 files changed

Lines changed: 35 additions & 23 deletions

File tree

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: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ def to_h
8585
next if object.nil?
8686

8787
# If it's a plural msgstr
88-
if object.value.is_a?(Array)
88+
if object.is_a?(Array)
89+
hash[:msgstr] = object.map(&:to_s).compact
90+
# If it's a multiline msgstr
91+
elsif object.value.is_a?(Array)
8992
hash[object.type] = object.value.compact
9093
else
9194
hash[object.type] = object.to_s
@@ -120,9 +123,8 @@ def set_instance_variable(name, value)
120123
elsif ENTRIES_LABELS.include? name
121124
instance_variable_set "@#{name}".to_sym, Message.new(name, value)
122125
elsif /^msgstr\[[0-9]\]/.match?(name.to_s)
123-
# If it's a plural msgstr
124-
@msgstr ||= []
125-
@msgstr << Message.new(name, value)
126+
# If it's a plural msgstr, change instance variable name to @msgstr_n as @msgstr[n] is not a valid variable name
127+
instance_variable_set "@msgstr_#{plural_form(name)}".to_sym, Message.new(name, value)
126128
end
127129
end
128130

@@ -142,6 +144,10 @@ def define_writer_method(type, object)
142144
klass = instance_variable_get "@#{type}".to_sym
143145
klass.type = type
144146
klass.value = val
147+
elsif type.match(/^msgstr_\d/)
148+
plural_form = type.to_s.scan(/^msgstr_(\d)/).last.first.to_i
149+
object_type = "msgstr[#{plural_form}]".to_sym
150+
instance_variable_set "@#{type}".to_sym, object.new(object_type, val)
145151
else
146152
instance_variable_set "@#{type}".to_sym, object.new(type, val)
147153
end
@@ -199,5 +205,9 @@ def define_aliases
199205
self.class.send(:alias_method, :refrence, :reference)
200206
self.class.send(:alias_method, :refrence=, :reference=)
201207
end
208+
209+
def plural_form(name)
210+
name.to_s.scan(/^msgstr\[([0-9])\]/).last.first.to_i
211+
end
202212
end
203213
end

lib/poparser/message.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +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'
2523

2624
# multiline messages should be started with an empty line
2725
lines = ["#{label} \"\"\n"]
@@ -50,14 +48,6 @@ def remove_empty_line
5048
end
5149
end
5250

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-
6151
def label
6252
if /msgstr\[[0-9]\]/.match?(@type.to_s)
6353
@type

spec/poparser/entry_spec.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,23 @@
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+
45+
# args = {
46+
# :translator_comment => ['comment', 'second line of comments'],
47+
# :msgid_plural => 'right word',
48+
# 'msgstr[0]': %w[mot juste],
49+
# 'msgstr[1]': %w[mots justes]
50+
# }
51+
# @entry = PoParser::Entry.new(args)
4352
result = {
4453
:translator_comment => ['comment', 'second line of comments'],
45-
:msgid_plural => 'word',
46-
:msgstr => %w[mot mots]
54+
:msgid_plural => 'right word',
55+
'msgstr[0]': %w[mot juste],
56+
'msgstr[1]': %w[mots justes]
4757
}
4858
expect(@entry.to_h).to eq(result)
4959
end
@@ -103,7 +113,7 @@
103113
@entry.flag = 'fuzzy'
104114
@entry.msgid = ['first line', 'second line']
105115
@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"
116+
result = "#, fuzzy\nmsgid \"\"\n\"first line\"\n\"second line\"\nmsgstr \"\"\n\"first line\"\n\"second line\"\n"
107117
expect(@entry.to_s).to eq(result)
108118
end
109119
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)