Skip to content

Commit

Permalink
Google Translator: Handles line breaks
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
davidwessman authored and glebm committed May 10, 2024
1 parent dffd911 commit 7bf71a5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
27 changes: 26 additions & 1 deletion lib/i18n/tasks/translators/google_translator.rb
Expand Up @@ -4,6 +4,7 @@

module I18n::Tasks::Translators
class GoogleTranslator < BaseTranslator
NEWLINE_PLACEHOLDER = '<br id=i18n />'
def initialize(*)
begin
require 'easy_translate'
Expand All @@ -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]
)

Check failure on line 28 in lib/i18n/tasks/translators/google_translator.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/TrailingWhitespace: Trailing whitespace detected.
end

def options_for_translate_values(from:, to:, **options)
Expand Down Expand Up @@ -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
22 changes: 14 additions & 8 deletions spec/google_translate_spec.rb
Expand Up @@ -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, <b>%{user} O'neill</b>", "Hola, <b>%{user} O'neill</b>"]
html_test_plrl = ['html-key.html.one', '<b>Hello %{count}</b>', '<b>Hola %{count}</b>']
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, <b>%{user} O'neill</b>", "Hola, <b>%{user} O'neill</b>"]
html_test_plrl = ['html-key.html.one', '<b>Hello %{count}</b>', '<b>Hola %{count}</b>']
html_test_multiline = ['html-key.html.multiline', "<b>Hello</b>\n<b>%{user}</b>", "<b>Hola</b>\n<b>%{user}</b>"]
array_test = ['array-key', ['Hello.', nil, '', 'Goodbye.'], ['Hola.', nil, '', 'Adiós.']]
fixnum_test = ['numeric-key', 1, 1]

Check failure on line 15 in spec/google_translate_spec.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/ExtraSpacing: Unnecessary spacing detected.

Check failure on line 15 in spec/google_translate_spec.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/SpaceAroundOperators: Operator = should be surrounded by a single space.
ref_key_test = ['ref-key', :reference, :reference]

Check failure on line 16 in spec/google_translate_spec.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/ExtraSpacing: Unnecessary spacing detected.

describe 'real world test' do
delegate :i18n_task, :in_test_app_dir, :run_cmd, to: :TestCodebase
Expand All @@ -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],
Expand All @@ -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])
Expand Down

0 comments on commit 7bf71a5

Please sign in to comment.