Skip to content

Commit

Permalink
Allow aliases for deepl locales
Browse files Browse the repository at this point in the history
If you only use the short locale name internally, you can use this commit in deepl to force a specific variant of the locale.
  • Loading branch information
georf committed Nov 20, 2024
1 parent eefc659 commit 69f1971
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
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

0 comments on commit 69f1971

Please sign in to comment.