From cecd72c95aa96ad6697177a4e4251f794811407f Mon Sep 17 00:00:00 2001 From: Rod Murphy Date: Tue, 15 Jun 2021 09:38:34 +0100 Subject: [PATCH] Create plural string msgstr instance variables in Entry class --- lib/poparser/constants.rb | 4 +++- lib/poparser/entry.rb | 15 +++++++++++---- lib/poparser/message.rb | 11 ----------- spec/poparser/entry_spec.rb | 13 ++++++++----- spec/poparser/po_spec.rb | 6 +++--- 5 files changed, 25 insertions(+), 24 deletions(-) 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 c2354d6..9fdce0a 100644 --- a/lib/poparser/entry.rb +++ b/lib/poparser/entry.rb @@ -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 @@ -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/lib/poparser/message.rb b/lib/poparser/message.rb index a8c80c5..860f70f 100644 --- a/lib/poparser/message.rb +++ b/lib/poparser/message.rb @@ -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| @@ -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 diff --git a/spec/poparser/entry_spec.rb b/spec/poparser/entry_spec.rb index c7e6784..a6d3d0c 100644 --- a/spec/poparser/entry_spec.rb +++ b/spec/poparser/entry_spec.rb @@ -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 @@ -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 diff --git a/spec/poparser/po_spec.rb b/spec/poparser/po_spec.rb index 3d5032d..f214cc1 100644 --- a/spec/poparser/po_spec.rb +++ b/spec/poparser/po_spec.rb @@ -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