Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Translator: Handles line breaks #567

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion lib/i18n/tasks/translators/google_translator.rb
Original file line number Diff line number Diff line change
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 @@
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 @@
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
Original file line number Diff line number Diff line change
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 @@ -27,7 +29,7 @@
context 'command' do
let(:task) { i18n_task }

it 'works' do

Check failure on line 32 in spec/google_translate_spec.rb

View workflow job for this annotation

GitHub Actions / lint

RSpec/MultipleExpectations: Example has too many expectations [11/9].
skip 'temporarily disabled on JRuby due to https://github.com/jruby/jruby/issues/4802' if RUBY_ENGINE == 'jruby'
skip 'GOOGLE_TRANSLATE_API_KEY env var not set' unless ENV['GOOGLE_TRANSLATE_API_KEY']
skip 'GOOGLE_TRANSLATE_API_KEY env var is empty' if ENV['GOOGLE_TRANSLATE_API_KEY'].empty?
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
Loading