Check newlines in translations

This commit is contained in:
Bob Van Landuyt 2017-08-24 19:11:35 +02:00
parent 973c697960
commit 1da594d39b
3 changed files with 41 additions and 2 deletions

View file

@ -61,6 +61,10 @@ module Gitlab
if entry[:msgid].is_a?(Array) if entry[:msgid].is_a?(Array)
errors << "<#{message_id}> is defined over multiple lines, this breaks some tooling." errors << "<#{message_id}> is defined over multiple lines, this breaks some tooling."
end 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 end
def validate_variables(errors, entry) def validate_variables(errors, entry)
@ -172,5 +176,17 @@ module Gitlab
def join_message(message) def join_message(message)
Array(message).join Array(message).join
end 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
end end

View file

@ -30,3 +30,12 @@ msgstr ""
"Va a eliminar %{group_name}.\n" "Va a eliminar %{group_name}.\n"
"¡El grupo eliminado NO puede ser restaurado!\n" "¡El grupo eliminado NO puede ser restaurado!\n"
"¿Estás TOTALMENTE seguro?" "¿Estás TOTALMENTE seguro?"
msgid "With plural"
msgid_plural "with plurals"
msgstr[0] "first"
msgstr[1] "second"
msgstr[2] ""
"with"
"multiple"
"lines"

View file

@ -26,11 +26,25 @@ describe Gitlab::PoLinter do
context 'for a translations with newlines' do context 'for a translations with newlines' do
let(:po_path) { 'spec/fixtures/newlines.po' } 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?" 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." 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
end end