diff --git a/.gitignore b/.gitignore index 069a98c..b6386b9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ .yardoc .ruby-version .gitignore +.idea/ Gemfile.lock InstalledFiles _yardoc diff --git a/lib/poparser/constants.rb b/lib/poparser/constants.rb index 80bddc3..aba23b5 100644 --- a/lib/poparser/constants.rb +++ b/lib/poparser/constants.rb @@ -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 diff --git a/lib/poparser/entry.rb b/lib/poparser/entry.rb index 4822d68..9fdce0a 100644 --- a/lib/poparser/entry.rb +++ b/lib/poparser/entry.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/poparser/entry_spec.rb b/spec/poparser/entry_spec.rb index 60d33e7..07aae79 100644 --- a/spec/poparser/entry_spec.rb +++ b/spec/poparser/entry_spec.rb @@ -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' diff --git a/spec/poparser/po_spec.rb b/spec/poparser/po_spec.rb index 9e72dda..f214cc1 100644 --- a/spec/poparser/po_spec.rb +++ b/spec/poparser/po_spec.rb @@ -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