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

Allow aliases for deepl locales #609

Merged
merged 1 commit into from
Nov 20, 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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ GOOGLE_TRANSLATE_API_KEY=<Google Translate API key>
<a name="deepl-translation-config"></a>
### DeepL Pro Translate

`i18n-tasks translate-missing` requires a DeepL Pro API key, get it at [DeepL](https://www.deepl.com/pro).
`i18n-tasks translate-missing` requires a DeepL Pro API key, get it at [DeepL](https://www.deepl.com/pro). You can specify alias locales if you only use the simple locales internally.

```yaml
# config/i18n-tasks.yml
Expand All @@ -436,6 +436,9 @@ translation:
- 2c6415be-1852-4f54-9e1b-d800463496b4
deepl_options:
formality: prefer_less
deepl_locale_aliases:
en: en-us
pt: pt-br
```

or via environment variables:
Expand Down
3 changes: 3 additions & 0 deletions lib/i18n/tasks/translators/deepl_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def to_deepl_source_locale(locale)

# Convert 'es-ES' to 'ES' but warn about locales requiring a specific variant
def to_deepl_target_locale(locale)
locale_aliases = @i18n_tasks.translation_config[:deepl_locale_aliases]
locale = locale_aliases[locale.to_s.downcase] || locale if locale_aliases.is_a?(Hash)

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
Expand Down
38 changes: 38 additions & 0 deletions spec/deepl_translate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,42 @@
end
end
end

describe 'locale aliases' do
delegate :i18n_task, :in_test_app_dir, :run_cmd, to: :TestCodebase
let(:en) { { en: { hello: 'Hello' } } }
let(:config) do
{ base_locale: 'en', locales: %w[pt], translation: { backend: 'deepl', deepl_locale_aliases: { pt: 'pt-br' } } }
end

before do
TestCodebase.setup(
'config/locales/en.yml' => en.to_yaml, 'config/locales/pt.yml' => '',
'config/i18n-tasks.yml' => config.to_yaml
)
end

after do
TestCodebase.teardown
end

context 'when configuration has pt=>pt-br' do
it 'uses pt-br instead of pt' do
skip 'DEEPL_AUTH_KEY env var not set' unless ENV['DEEPL_AUTH_KEY']

# rubocop:disable RSpec/StubbedMock
expect(DeepL).to receive(:translate).with(
['Hello'],
'EN',
'PT-BR',
{ html_escape: true, ignore_tags: ['i18n'], preserve_formatting: true, tag_handling: 'xml' }
).and_raise('correct')
# rubocop:enable RSpec/StubbedMock

expect do
TestCodebase.run_cmd 'translate-missing'
end.to raise_error('correct')
end
end
end
end