@@ -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
203213end
0 commit comments