From 1da594d39b4b5d6d905ab9a8325d694b3b0fbec7 Mon Sep 17 00:00:00 2001 From: Bob Van Landuyt Date: Thu, 24 Aug 2017 19:11:35 +0200 Subject: [PATCH] Check newlines in translations --- lib/gitlab/po_linter.rb | 16 ++++++++++++++++ spec/fixtures/newlines.po | 9 +++++++++ spec/lib/gitlab/po_linter_spec.rb | 18 ++++++++++++++++-- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/po_linter.rb b/lib/gitlab/po_linter.rb index 721a111e2a0..44abea640c3 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 515d0b3ba99..773d9b23db8 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 75b3163753f..74a3d8b95f8 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