Skip to content

Commit

Permalink
Create plural string msgstr instance variables in Entry class
Browse files Browse the repository at this point in the history
  • Loading branch information
rod-murphy committed Jun 15, 2021
1 parent 629073d commit cecd72c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
4 changes: 3 additions & 1 deletion lib/poparser/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ module PoParser
msgid: 'msgid',
msgid_plural: 'msgid_plural',
msgstr: 'msgstr',
}.freeze
}
(0..9).to_a.each { |index| ENTRIES_LABELS["msgstr_#{index}".to_sym] = "msgstr[#{index}]" }
ENTRIES_LABELS.freeze

LABELS = COMMENTS_LABELS.merge(ENTRIES_LABELS).keys

Expand Down
15 changes: 11 additions & 4 deletions lib/poparser/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def to_h
object = instance_variable_get(label)
next if object.nil?

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

Expand All @@ -142,6 +141,10 @@ def define_writer_method(type, object)
klass = instance_variable_get "@#{type}".to_sym
klass.type = type
klass.value = val
elsif type.match(/^msgstr_\d/)
plural_form = type.to_s.scan(/^msgstr_(\d)/).last.first.to_i
object_type = "msgstr[#{plural_form}]".to_sym
instance_variable_set "@#{type}".to_sym, object.new(object_type, val)
else
instance_variable_set "@#{type}".to_sym, object.new(type, val)
end
Expand Down Expand Up @@ -199,5 +202,9 @@ def define_aliases
self.class.send(:alias_method, :refrence, :reference)
self.class.send(:alias_method, :refrence=, :reference=)
end

def plural_form(name)
name.to_s.scan(/^msgstr\[([0-9])\]/).last.first.to_i
end
end
end
11 changes: 0 additions & 11 deletions lib/poparser/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ def to_s(with_label = false)

if @value.is_a? Array
remove_empty_line
# special case for plural strings
return msgstr_plural_to_s if label == 'msgstr'

# multiline messages should be started with an empty line
lines = ["#{label} \"\"\n"]
@value.each do |str|
Expand Down Expand Up @@ -50,14 +47,6 @@ def remove_empty_line
end
end

def msgstr_plural_to_s
lines = []
@value.each_with_index do |str, index|
lines << "msgstr[#{index}] \"#{str}\"\n"
end
lines.join
end

def label
if /msgstr\[[0-9]\]/.match?(@type.to_s)
@type
Expand Down
13 changes: 8 additions & 5 deletions spec/poparser/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@

it 'should show a hash presentation of a plural string entry' do
@entry = PoParser::Entry.new
@entry.msgid_plural = 'word'
@entry.msgstr = %w[mot mots]
@entry.msgid_plural = 'right word'
@entry.msgstr_0 = %w[mot juste]
@entry.msgstr_1 = %w[mots justes]
@entry.translator_comment = ['comment', 'second line of comments']

result = {
:translator_comment => ['comment', 'second line of comments'],
:msgid_plural => 'word',
:msgstr => %w[mot mots]
:msgid_plural => 'right word',
:'msgstr[0]' => %w[mot juste],
:'msgstr[1]' => %w[mots justes]
}
expect(@entry.to_h).to eq(result)
end
Expand Down Expand Up @@ -103,7 +106,7 @@
@entry.flag = 'fuzzy'
@entry.msgid = ['first line', 'second line']
@entry.msgstr = ['first line', 'second line']
result = "#, fuzzy\nmsgid \"\"\n\"first line\"\n\"second line\"\nmsgstr[0] \"first line\"\nmsgstr[1] \"second line\"\n"
result = "#, fuzzy\nmsgid \"\"\n\"first line\"\n\"second line\"\nmsgstr \"\"\n\"first line\"\n\"second line\"\n"
expect(@entry.to_s).to eq(result)
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/poparser/po_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
'msgstr[1]': 'phrases',
}
@po << {
msgid_plural: 'word',
msgstr: %w[word words]
msgid: 'multiline word',
msgstr: %w[line1 line2]
}
entry_1 = "# comment\n# another comment line\n#: reference comment\nmsgid \"untranslated\"\nmsgstr \"translated string\"\n"
entry_2 = "msgid_plural \"phrase\"\nmsgstr[0] \"phrase\"\nmsgstr[1] \"phrases\"\n"
entry_3 = "msgid_plural \"word\"\nmsgstr[0] \"word\"\nmsgstr[1] \"words\"\n"
entry_3 = "msgid \"multiline word\"\nmsgstr \"\"\n\"line1\"\n\"line2\"\n"

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

0 comments on commit cecd72c

Please sign in to comment.