Skip to content
Merged
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
18 changes: 10 additions & 8 deletions lib/simple_po_parser/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def msgctxt
skip_whitespace
text = message_line
add_result(:msgctxt, text)
message_multiline(:msgctxt) if text.empty?
message_multiline(:msgctxt) if @scanner.peek(1) == '"'
end
msgid
rescue PoSyntaxError => pe
Expand All @@ -127,7 +127,7 @@ def msgid
skip_whitespace
text = message_line
add_result(:msgid, text)
message_multiline(:msgid) if text.empty?
message_multiline(:msgid) if @scanner.peek(1) == '"'
if msgid_plural
msgstr_plural
else
Expand Down Expand Up @@ -155,7 +155,7 @@ def msgid_plural
skip_whitespace
text = message_line
add_result(:msgid_plural, text)
message_multiline(:msgid_plural) if text.empty?
message_multiline(:msgid_plural) if @scanner.peek(1) == '"'
true
else
false
Expand All @@ -174,7 +174,7 @@ def msgstr
skip_whitespace
text = message_line
add_result(:msgstr, text)
message_multiline(:msgstr) if text.empty?
message_multiline(:msgstr) if @scanner.peek(1) == '"'
skip_whitespace
raise PoSyntaxError, "Unexpected content after expected message end #{@scanner.peek(10).inspect}" unless @scanner.eos?
else
Expand Down Expand Up @@ -202,7 +202,7 @@ def msgstr_plural(num = 0)
skip_whitespace
text = message_line
add_result(msgstr_key, text)
message_multiline(msgstr_key) if text.empty?
message_multiline(msgstr_key) if @scanner.peek(1) == '"'
msgstr_plural(num+1)
elsif num == 0 # and msgstr_key was false
raise PoSyntaxError, "Plural message without msgstr[0] is not allowed. Line started unexpectedly with #{@scanner.peek(10).inspect}."
Expand Down Expand Up @@ -238,7 +238,7 @@ def previous_comments
skip_whitespace
text = message_line
add_result(key, text)
previous_multiline(key) if text.empty?
previous_multiline(key) if @scanner.match?(/#\|\p{Blank}*"/)
else
raise PoSyntaxError, "Previous comments must start with '#| msg'. #{@scanner.peek(10).inspect} unknown."
end
Expand All @@ -265,8 +265,10 @@ def previous_multiline(key)

# parses a multiline message
#
# multiline messages are indicated by an empty content as first line and the next line
# starting with the double quote character
# Multiline messages are usually indicated by an empty string as the first line,
# followed by more lines starting with the double quote character.
#
# However, according to the PO file standard, the first line can also contain content.
def message_multiline(key)
begin
skip_whitespace
Expand Down
10 changes: 10 additions & 0 deletions spec/simple_po_parser/fixtures/multiline.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#| msgid "multiline\n"
#|"previous messageid"
#|"with non-empty first line"
msgid ""
"multiline string "
"with empty first line "
"and trailing spaces"
msgstr "multiline string"
"with non-empty first line"
"and no trailing spaces"
10 changes: 10 additions & 0 deletions spec/simple_po_parser/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
let (:po_header) { File.read(File.expand_path("fixtures/header.po", __dir__))}
let(:po_complex_message) { File.read(File.expand_path("fixtures/complex_entry.po", __dir__))}
let(:po_simple_message) { File.read(File.expand_path("fixtures/simple_entry.po", __dir__))}
let(:po_multiline_message) { File.read(File.expand_path("fixtures/multiline.po", __dir__))}

it "parses the PO header" do
expected_result = {
Expand All @@ -27,6 +28,15 @@
expect(SimplePoParser::Parser.new.parse(po_simple_message)).to eq(expected_result)
end

it "parses the multiline entry as expected" do
expected_result = {
:msgid => ["", "multiline string ", "with empty first line ", "and trailing spaces"],
:msgstr => ["multiline string", "with non-empty first line", "and no trailing spaces"],
:previous_msgid => ["multiline\\n", "previous messageid", "with non-empty first line"],
}
expect(SimplePoParser::Parser.new.parse(po_multiline_message)).to eq(expected_result)
end

it "parses the complex entry as expected" do
expected_result = {
:translator_comment => ["translator-comment", ""],
Expand Down