diff --git a/lib/i18n/tasks/translators/google_translator.rb b/lib/i18n/tasks/translators/google_translator.rb index 0890e922..686544ef 100644 --- a/lib/i18n/tasks/translators/google_translator.rb +++ b/lib/i18n/tasks/translators/google_translator.rb @@ -21,7 +21,7 @@ def translate_values(list, **options) EasyTranslate.translate( replace_newlines_with_placeholder(list, options[:html]), options, - format: :text + format: options[:html] ? :html : :text ), options[:html] ) @@ -69,7 +69,9 @@ def replace_newlines_with_placeholder(list, html) return list unless html list.map do |value| - value.gsub("\n", NEWLINE_PLACEHOLDER) + value.gsub(/\n(\s*)/) do + "" + end end end @@ -77,7 +79,9 @@ def restore_newlines(translations, html) return translations unless html translations.map do |translation| - translation.gsub("#{NEWLINE_PLACEHOLDER} ", "\n") + translation.gsub(//) do + "\n#{' ' * ::Regexp.last_match(1).to_i}" + end end end end diff --git a/spec/google_translate_spec.rb b/spec/google_translate_spec.rb index 4ecb867e..e06c43bd 100644 --- a/spec/google_translate_spec.rb +++ b/spec/google_translate_spec.rb @@ -7,10 +7,26 @@ 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!"] + text_test_multiline = [ + 'hello_multiline', + "Hello,\n%{user}\nO'Neill!", + "Hola,\n%{user}\nO'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}"] + html_test_multiline = [ + 'html-key.html.multiline_html', + "Hello\n%{user}", + "Hola\n %{user}" + ] + # Google Translate API adds extra spaces before some characters + # https://issuetracker.google.com/issues/119256504?pli=1 + # Atleast it should be valid HTML + html_test_multiline_indentation = [ + 'html-key.html.multiline_indentation_html', + "

Hello

\n