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: Improves newline handling #597

Merged
merged 1 commit into from
Sep 15, 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
10 changes: 7 additions & 3 deletions lib/i18n/tasks/translators/google_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)
Expand Down Expand Up @@ -69,15 +69,19 @@ 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
"<Z__#{::Regexp.last_match(1)&.length || 0}>"
end
end
end

def restore_newlines(translations, html)
return translations unless html

translations.map do |translation|
translation.gsub("#{NEWLINE_PLACEHOLDER} ", "\n")
translation.gsub(/<Z__(\d+)>/) do
"\n#{' ' * ::Regexp.last_match(1).to_i}"
end
end
end
end
Expand Down
59 changes: 39 additions & 20 deletions spec/google_translate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, <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>"]
html_test_multiline = [
'html-key.html.multiline_html',
"<b>Hello</b>\n<b>%{user}</b>",
"<b>Hola</b>\n <b>%{user}</b>"
]
# 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',
"<p>Hello</p>\n<ul>\n <li>%{user}</li>\n <li>\n %{user2}\n </li>\n <li>Dog</li>\n<ul>\n",
"<p>Hola</p>\n<ul>\n <li> %{user}</li>\n <li>\n %{user2}\n </li>\n <li> Perro</li>\n<ul>\n"
]
array_test = ['array-key', ['Hello.', nil, '', 'Goodbye.'], ['Hola.', nil, '', 'Adiós.']]
fixnum_test = ['numeric-key', 1, 1]
ref_key_test = ['ref-key', :reference, :reference]
Expand All @@ -30,27 +46,29 @@
let(:task) { i18n_task }

it 'works' do # rubocop:disable RSpec/MultipleExpectations
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?
in_test_app_dir do
task.data[:en] = build_tree('en' => {
'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],
'fixnum-key' => fixnum_test[1],
'ref-key' => ref_key_test[1]
}
})
task.data[:en] = build_tree(
'en' => {
'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],
'multiline_indentation_html' => html_test_multiline_indentation[1],
'array_key' => array_test[1],
'nil-value-key' => nil_value_test[1],
'empty-value-key' => empty_value_test[1],
'fixnum-key' => fixnum_test[1],
'ref-key' => ref_key_test[1]
}
}
)
task.data[:es] = build_tree('es' => {
'common' => {
'a' => 'λ'
Expand All @@ -63,6 +81,7 @@
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.multiline_indentation_html', 'es')).to eq(html_test_multiline_indentation[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