Skip to content

Commit

Permalink
Closes #575 - Allow configuring translation backend in yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredmoody committed May 24, 2024
1 parent 9852a3f commit 3e59981
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 38 deletions.
49 changes: 13 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,49 +84,22 @@ Usage: i18n-tasks add-missing [options] [locale ...]
-h, --help Display this help message.
```

### Google Translate missing keys
### Translate Missing Keys

Translate missing values with Google Translate ([more below on the API key](#google-translation-config)).
Translate missing keys using a backend service of your choice.

```console
$ i18n-tasks translate-missing

# accepts from and locales options:
$ i18n-tasks translate-missing --from=base es fr
# accepts backend, from and locales options
$ i18n-tasks translate-missing --from=base es fr --backend=google
```

### DeepL Pro Translate missing keys

Translate missing values with DeepL Pro Translate ([more below on the API key](#deepl-translation-config)).

```console
$ i18n-tasks translate-missing --backend=deepl

# accepts from and locales options:
$ i18n-tasks translate-missing --backend=deepl --from=en fr nl
```

### Yandex Translate missing keys

Translate missing values with Yandex Translate ([more below on the API key](#yandex-translation-config)).

```console
$ i18n-tasks translate-missing --backend=yandex

# accepts from and locales options:
$ i18n-tasks translate-missing --from=en es fr
```

### OpenAI Translate missing keys

Translate missing values with OpenAI ([more below on the API key](#openai-translation-config)).

```console
$ i18n-tasks translate-missing --backend=openai

# accepts from and locales options:
$ i18n-tasks translate-missing --from=en es fr
```
Available backends:
- `google` - [Google Translate](#google-translation-config)
- `deepl` - [DeepL Pro](#deepl-translation-config)
- `yandex` - [Yandex Translate](#yandex-translation-config)
- `openai` - [OpenAI](#openai-translation-config)

### Find usages

Expand Down Expand Up @@ -435,6 +408,7 @@ Put the key in `GOOGLE_TRANSLATE_API_KEY` environment variable or in the config
```yaml
# config/i18n-tasks.yml
translation:
backend: google
google_translate_api_key: <Google Translate API key>
```

Expand All @@ -452,6 +426,7 @@ GOOGLE_TRANSLATE_API_KEY=<Google Translate API key>
```yaml
# config/i18n-tasks.yml
translation:
backend: deepl
deepl_api_key: <DeepL Pro API key>
deepl_host: <optional>
deepl_version: <optional>
Expand All @@ -478,6 +453,7 @@ DEEPL_VERSION=<optional>
```yaml
# config/i18n-tasks.yml
translation:
backend: yandex
yandex_api_key: <Yandex API key>
```

Expand All @@ -495,6 +471,7 @@ YANDEX_API_KEY=<Yandex API key>
```yaml
# config/i18n-tasks.yml
translation:
backend: openai
openai_api_key: <OpenAI API key>
openai_model: <optional>
```
Expand Down
4 changes: 3 additions & 1 deletion lib/i18n/tasks/command/commands/missing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def translate_missing(opt = {})
pattern_re = i18n.compile_key_pattern(opt[:pattern])
missing.select_keys! { |full_key, _node| full_key =~ pattern_re }
end
translated = i18n.translate_forest missing, from: opt[:from], backend: opt[:backend].to_sym

backend = i18n.config.dig(:translation, :backend) || opt[:backend]
translated = i18n.translate_forest missing, from: opt[:from], backend: backend.to_sym
i18n.data.merge! translated
log_stderr t('i18n_tasks.translate_missing.translated', count: translated.leaves.count)
print_forest translated, opt
Expand Down
13 changes: 12 additions & 1 deletion spec/commands/missing_commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

around do |ex|
TestCodebase.setup(
'config/i18n-tasks.yml' => { base_locale: 'en', locales: %w[es fr] }.to_yaml,
'config/i18n-tasks.yml' => { base_locale: 'en', locales: %w[es fr], translation: { backend: 'deepl' } }.to_yaml,
'config/locales/es.yml' => { 'es' => missing_keys }.to_yaml
)
TestCodebase.in_test_app_dir { ex.call }
Expand Down Expand Up @@ -47,4 +47,15 @@
end
end
end

describe '#translate_missing' do
it 'uses the backend from the configuration' do
deepl_double = instance_double(I18n::Tasks::Translators::DeeplTranslator)
allow(I18n::Tasks::Translators::DeeplTranslator).to receive(:new).and_return(deepl_double)
allow(deepl_double).to receive(:translate_forest).and_return(I18n::Tasks::BaseTask.new.empty_forest)
expect(deepl_double).to receive(:translate_forest)

run_cmd 'translate-missing'
end
end
end

0 comments on commit 3e59981

Please sign in to comment.