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

Send DeepL translation requests in multiple batches #474

Merged
merged 3 commits into from
Sep 26, 2022
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
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ en:
Setup DeepL Pro API key via DEEPL_AUTH_KEY environment variable or translation.deepl_api_key
in config/i18n-tasks.yml. Get the key at https://www.deepl.com/pro.
no_results: DeepL returned no results.
specific_target_missing: You must supply a specific variant for the given target language
e.g. en-us instead of en.
google_translate:
errors:
no_api_key: >-
Expand Down
2 changes: 2 additions & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ ru:
Задайте ключ API DeepL через переменную окружения DEEPL_AUTH_KEY или translation.deepl_api_key
Получите ключ через https://www.deepl.com/pro.
no_results: DeepL не дал результатов.
specific_target_missing: You must supply a specific variant for the given target language
e.g. en-us instead of en.
google_translate:
errors:
no_api_key: >-
Expand Down
35 changes: 28 additions & 7 deletions lib/i18n/tasks/translators/deepl_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

module I18n::Tasks::Translators
class DeeplTranslator < BaseTranslator
# max allowed texts per request
BATCH_SIZE = 50
glebm marked this conversation as resolved.
Show resolved Hide resolved
# those languages must be specified with their sub-kind e.g en-us
SPECIFIC_TARGETS= ['en', 'pt'].freeze

def initialize(*)
begin
require 'deepl'
Expand All @@ -17,12 +22,16 @@ def initialize(*)
protected

def translate_values(list, from:, to:, **options)
result = DeepL.translate(list, to_deepl_compatible_locale(from), to_deepl_compatible_locale(to), options)
if result.is_a?(DeepL::Resources::Text)
[result.text]
else
result.map(&:text)
results = []
list.each_slice(BATCH_SIZE) do |parts|
res = DeepL.translate(parts, to_deepl_source_locale(from), to_deepl_target_locale(to), options)
if res.is_a?(DeepL::Resources::Text)
results << res.text
else
results += res.map(&:text)
end
end
results
end

def options_for_translate_values(**options)
Expand Down Expand Up @@ -60,11 +69,23 @@ def no_results_error_message

private

# Convert 'es-ES' to 'ES'
def to_deepl_compatible_locale(locale)
# Convert 'es-ES' to 'ES', en-us to EN
def to_deepl_source_locale(locale)
locale.to_s.split('-', 2).first.upcase
end

# Convert 'es-ES' to 'ES' but warn about locales requiring a specific variant
def to_deepl_target_locale(locale)
loc, sub = locale.to_s.split('-')
if SPECIFIC_TARGETS.include?(loc)
# Must see how the deepl api evolves, so this could be an error in the future
@i18n_tasks.warn_deprecated I18n.t('i18n_tasks.deepl_translate.errors.specific_target_missing') unless sub
locale.to_s.upcase
else
loc.upcase
end
end

def configure_api_key!
api_key = @i18n_tasks.translation_config[:deepl_api_key]
host = @i18n_tasks.translation_config[:deepl_host]
Expand Down
8 changes: 5 additions & 3 deletions spec/deepl_translate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

RSpec.describe 'DeepL Translation' do
nil_value_test = ['nil-value-key', nil, nil]
text_test = ['key', "Hello, %{user} O'Neill! How are you?", "Hola, %{user} ¡O'Neill! Como estas?"]
html_test = ['html-key.html', "Hello, <b>%{user} big O'neill</b> ❤︎", "Hola, <b>%{user} gran O'neill</b> ❤︎"]
text_test = ['key', "Hello, %{user} O'Neill! How are you?", "¡Hola, %{user} O'Neill! ¿Cómo estás?"]
html_test_plrl = ['html-key.html.one', '<span>Hello %{count}</span>', '<span>Hola %{count}</span>']
array_test = ['array-key', ['Hello.', nil, '', 'Goodbye.'], ['Hola.', nil, '', 'Adiós.']]
fixnum_test = ['numeric-key', 1, 1]
ref_key_test = ['ref-key', :reference, :reference]
# this test fails atm due to moving of the bold tag => "Hola, <b>%{user} </b> gran O'neill ❤︎ "
# it could be a bug, but the api also allows to ignore certain tags and there is the new html-markup version which could be used to
html_test = ['html-key.html', "Hello, <b>%{user} big O'neill</b> ❤︎", "Hola, <b>%{user} gran O'neill</b> ❤︎ "]

describe 'real world test' do
delegate :i18n_task, :in_test_app_dir, :run_cmd, to: :TestCodebase
Expand Down Expand Up @@ -53,13 +55,13 @@

run_cmd 'translate-missing', '--backend=deepl'
expect(task.t('common.hello', 'es')).to eq(text_test[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.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.fixnum-key', 'es')).to eq(fixnum_test[2])
expect(task.t('common.ref-key', 'es')).to eq(ref_key_test[2])
expect(task.t('common.a', 'es')).to eq('λ')
expect(task.t('common.hello_html', 'es')).to eq(html_test[2])
end
end
end
Expand Down