diff --git a/README.md b/README.md index cae18e7e..140c52a0 100644 --- a/README.md +++ b/README.md @@ -422,7 +422,7 @@ GOOGLE_TRANSLATE_API_KEY= ### 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 @@ -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: diff --git a/lib/i18n/tasks/translators/deepl_translator.rb b/lib/i18n/tasks/translators/deepl_translator.rb index 644b5afc..a84df809 100644 --- a/lib/i18n/tasks/translators/deepl_translator.rb +++ b/lib/i18n/tasks/translators/deepl_translator.rb @@ -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 diff --git a/spec/deepl_translate_spec.rb b/spec/deepl_translate_spec.rb index baad36df..1df3c378 100644 --- a/spec/deepl_translate_spec.rb +++ b/spec/deepl_translate_spec.rb @@ -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