Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix how Entry#to_h represents Po entries with multiple lines #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.yardoc
.ruby-version
.gitignore
.idea/
Gemfile.lock
InstalledFiles
_yardoc
Expand Down
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
25 changes: 16 additions & 9 deletions lib/poparser/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ def flag_as(flag)
def to_h
instance_variables.each_with_object({}) do |label, hash|
object = instance_variable_get(label)
# If it's a plural msgstr
if object.is_a?(Array)
object.each do |entry|
hash[entry.type] = entry.to_s unless entry.nil?
end
next if object.nil?

# If it's a multiline message/comment
if object.value.is_a?(Array)
hash[object.type] = object.value.compact
else
hash[object.type] = object.to_s unless object.nil?
hash[object.type] = object.to_s
end
end
end
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
25 changes: 24 additions & 1 deletion spec/poparser/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,35 @@
}.to raise_error(ArgumentError, "Unknown label blah_blah")
end

it 'should show a hash presentation of a entry' do
it 'should show a hash presentation of an entry' do
@entry.msgid = 'string'
@entry.msgstr = 'reshte'
expect(@entry.to_h).to eq({:msgid=>"string", :msgstr=>"reshte"})
end

it 'should show a hash representation of a complex entry' do
# Ensure the to_h method is reversible
# From SimplePoParser::Parser - https://github.com/experteer/simple_po_parser/blob/v1.1.5/spec/simple_po_parser/parser_spec.rb#L31
entry_hash = {
:translator_comment => ["translator-comment", ""],
:extracted_comment => "extract",
:reference => ["reference1", "reference2"],
:flag => "flag",
:previous_msgctxt => "previous context",
:previous_msgid => ["", "multiline\\n", "previous messageid"],
:previous_msgid_plural => "previous msgid_plural",
:msgctxt => "Context",
:msgid => "msgid",
:msgid_plural => ["", "multiline msgid_plural\\n", ""],
"msgstr[0]" => "msgstr 0",
"msgstr[1]" => ["", "msgstr 1 multiline 1\\n", "msgstr 1 line 2\\n"],
"msgstr[2]" => "msgstr 2"
}

entry = PoParser::Entry.new(entry_hash)
expect(entry.to_h).to eq(entry_hash)
end

it 'should translate the entry' do
@entry.translate ('this entry is translated')
expect(@entry.msgstr.to_s).to eq 'this entry is translated'
Expand Down
23 changes: 23 additions & 0 deletions spec/poparser/po_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@
@po = PoParser::Po.new
end

it 'should output the po to a string correctly' do
@po << {
translator_comment: ['comment', 'another comment line'],
reference: 'reference comment',
msgid: 'untranslated',
msgstr: 'translated string'
}
@po << {
msgid_plural: 'phrase',
'msgstr[0]': 'phrase',
'msgstr[1]': 'phrases',
}
@po << {
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 \"multiline word\"\nmsgstr \"\"\n\"line1\"\n\"line2\"\n"

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

it 'should be able to add an entry to Po' do
# << is an alias for Po#add
expect(@po << entry).to be_a_kind_of PoParser::Po
Expand Down