From 942ad229ffc3d650b316757fcbb12daada89da0b Mon Sep 17 00:00:00 2001 From: David Wessman Date: Thu, 9 May 2024 21:19:16 +0200 Subject: [PATCH] Google Translator: Handles line breaks - Line breaks are not kept when translating html content via Google Translate - Therefore we transform `\n` into a placeholder and then back after translating - Fixes #566 --- .../tasks/translators/google_translator.rb | 27 ++++++++++++++++++- spec/google_translate_spec.rb | 22 +++++++++------ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/lib/i18n/tasks/translators/google_translator.rb b/lib/i18n/tasks/translators/google_translator.rb index 5cc8f05a..0651a6ec 100644 --- a/lib/i18n/tasks/translators/google_translator.rb +++ b/lib/i18n/tasks/translators/google_translator.rb @@ -4,6 +4,7 @@ module I18n::Tasks::Translators class GoogleTranslator < BaseTranslator + NEWLINE_PLACEHOLDER = '
' def initialize(*) begin require 'easy_translate' @@ -16,7 +17,15 @@ def initialize(*) protected def translate_values(list, **options) - EasyTranslate.translate(list, options) + restore_newlines( + EasyTranslate.translate( + replace_newlines_with_placeholder(list, options[:html]), + options, + format: :text + ), + options[:html] + ) + end def options_for_translate_values(from:, to:, **options) @@ -56,5 +65,21 @@ def api_key key end end + + def replace_newlines_with_placeholder(list, html) + return list unless html + + list.map do |value| + value.gsub("\n", NEWLINE_PLACEHOLDER) + end + end + + def restore_newlines(translations, html) + return translations unless html + + translations.map do |translation| + translation.gsub("#{NEWLINE_PLACEHOLDER} ", "\n") + end + end end end diff --git a/spec/google_translate_spec.rb b/spec/google_translate_spec.rb index ab6ae61c..6f9f5ba2 100644 --- a/spec/google_translate_spec.rb +++ b/spec/google_translate_spec.rb @@ -4,14 +4,16 @@ require 'i18n/tasks/commands' RSpec.describe 'Google Translation' do - nil_value_test = ['nil-value-key', nil, nil] - empty_value_test = ['empty-value-key', '', ''] - text_test = ['key', "Hello, %{user} O'Neill!", "¡Hola, %{user} O'Neill!"] - html_test = ['html-key.html', "Hello, %{user} O'neill", "Hola, %{user} O'neill"] - html_test_plrl = ['html-key.html.one', 'Hello %{count}', 'Hola %{count}'] - array_test = ['array-key', ['Hello.', nil, '', 'Goodbye.'], ['Hola.', nil, '', 'Adiós.']] - fixnum_test = ['numeric-key', 1, 1] - ref_key_test = ['ref-key', :reference, :reference] + nil_value_test = ['nil-value-key', nil, nil] + empty_value_test = ['empty-value-key', '', ''] + text_test = ['hello', "Hello, %{user} O'Neill!", "¡Hola, %{user} O'Neill!"] + text_test_multiline = ['hello_multiline', "Hello,\n%{user}\nO'Neill!", "Hola,\n%{user}\n¡O'Neill!"] + html_test = ['html-key.html', "Hello, %{user} O'neill", "Hola, %{user} O'neill"] + html_test_plrl = ['html-key.html.one', 'Hello %{count}', 'Hola %{count}'] + html_test_multiline = ['html-key.html.multiline', "Hello\n%{user}", "Hola\n%{user}"] + array_test = ['array-key', ['Hello.', nil, '', 'Goodbye.'], ['Hola.', nil, '', 'Adiós.']] + fixnum_test = ['numeric-key', 1, 1] + ref_key_test = ['ref-key', :reference, :reference] describe 'real world test' do delegate :i18n_task, :in_test_app_dir, :run_cmd, to: :TestCodebase @@ -36,10 +38,12 @@ 'common' => { 'a' => 'λ', 'hello' => text_test[1], + 'hello_multiline' => text_test_multiline[1], 'hello_html' => html_test[1], 'hello_plural_html' => { 'one' => html_test_plrl[1] }, + 'hello_multiline_html' => html_test_multiline[1], 'array_key' => array_test[1], 'nil-value-key' => nil_value_test[1], 'empty-value-key' => empty_value_test[1], @@ -55,8 +59,10 @@ run_cmd 'translate-missing' expect(task.t('common.hello', 'es')).to eq(text_test[2]) + expect(task.t('common.hello_multiline', 'es')).to eq(text_test_multiline[2]) expect(task.t('common.hello_html', 'es')).to eq(html_test[2]) expect(task.t('common.hello_plural_html.one', 'es')).to eq(html_test_plrl[2]) + expect(task.t('common.hello_multiline_html', 'es')).to eq(html_test_multiline[2]) expect(task.t('common.array_key', 'es')).to eq(array_test[2]) expect(task.t('common.nil-value-key', 'es')).to eq(nil_value_test[2]) expect(task.t('common.empty-value-key', 'es')).to eq(empty_value_test[2])