diff --git a/lib/gitlab/po_linter.rb b/lib/gitlab/po_linter.rb index 721a111e2a0c99248435a875d15bf5691d868408..44abea640c3e63f1313022d056c5aee8e24b87e9 100644 --- a/lib/gitlab/po_linter.rb +++ b/lib/gitlab/po_linter.rb @@ -61,6 +61,10 @@ module Gitlab if entry[:msgid].is_a?(Array) errors << "<#{message_id}> is defined over multiple lines, this breaks some tooling." end + + if translations_in_entry(entry).any? { |translation| translation.is_a?(Array) } + errors << "<#{message_id}> has translations defined over multiple lines, this breaks some tooling." + end end def validate_variables(errors, entry) @@ -172,5 +176,17 @@ module Gitlab def join_message(message) Array(message).join end + + def translations_in_entry(entry) + if entry[:msgid_plural].present? + entry.fetch_values(*plural_translation_keys_in_entry(entry)) + else + [entry[:msgstr]] + end + end + + def plural_translation_keys_in_entry(entry) + entry.keys.select { |key| key =~ /msgstr\[\d*\]/ } + end end end diff --git a/spec/fixtures/newlines.po b/spec/fixtures/newlines.po index 515d0b3ba9917b994f499d224b8b12395160bcce..773d9b23db8561965689caa79756498a1c49d0ba 100644 --- a/spec/fixtures/newlines.po +++ b/spec/fixtures/newlines.po @@ -30,3 +30,12 @@ msgstr "" "Va a eliminar %{group_name}.\n" "¡El grupo eliminado NO puede ser restaurado!\n" "¿Estás TOTALMENTE seguro?" + +msgid "With plural" +msgid_plural "with plurals" +msgstr[0] "first" +msgstr[1] "second" +msgstr[2] "" +"with" +"multiple" +"lines" diff --git a/spec/lib/gitlab/po_linter_spec.rb b/spec/lib/gitlab/po_linter_spec.rb index 75b3163753fb4d840348f5da3ac691da0a013039..74a3d8b95f8564cc9e307c7eb312c4edffb7bb8c 100644 --- a/spec/lib/gitlab/po_linter_spec.rb +++ b/spec/lib/gitlab/po_linter_spec.rb @@ -26,11 +26,25 @@ describe Gitlab::PoLinter do context 'for a translations with newlines' do let(:po_path) { 'spec/fixtures/newlines.po' } - it 'has an error' do + it 'has an error for a normal string' do message_id = "You are going to remove %{group_name}.\\nRemoved groups CANNOT be restored!\\nAre you ABSOLUTELY sure?" expected_message = "<#{message_id}> is defined over multiple lines, this breaks some tooling." - is_expected.to include(message_id => [expected_message]) + expect(errors[message_id]).to include(expected_message) + end + + it 'has an error when a translation is defined over multiple lines' do + message_id = "You are going to remove %{group_name}.\\nRemoved groups CANNOT be restored!\\nAre you ABSOLUTELY sure?" + expected_message = "<#{message_id}> has translations defined over multiple lines, this breaks some tooling." + + expect(errors[message_id]).to include(expected_message) + end + + it 'raises an error when a plural translation is defined over multiple lines' do + message_id = 'With plural' + expected_message = "<#{message_id}> has translations defined over multiple lines, this breaks some tooling." + + expect(errors[message_id]).to include(expected_message) end end