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

Support DeepL glossaries #535

Merged
merged 1 commit into from
Nov 24, 2023
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: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ translation:
deepl_api_key: <DeepL Pro API key>
deepl_host: <optional>
deepl_version: <optional>
deepl_glossary_ids:
- f28106eb-0e06-489e-82c6-8215d6f95089
- 2c6415be-1852-4f54-9e1b-d800463496b4
deepl_options:
formality: prefer_less
```

or via environment variables:
Expand Down
28 changes: 27 additions & 1 deletion lib/i18n/tasks/translators/deepl_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ def initialize(*)
def translate_values(list, from:, to:, **options)
results = []
list.each_slice(BATCH_SIZE) do |parts|
res = DeepL.translate(parts, to_deepl_source_locale(from), to_deepl_target_locale(to), options)
res = DeepL.translate(
parts,
to_deepl_source_locale(from),
to_deepl_target_locale(to),
options_with_glossary(options, from, to)
)
if res.is_a?(DeepL::Resources::Text)
results << res.text
else
Expand Down Expand Up @@ -100,5 +105,26 @@ def configure_api_key!
config.version = version unless version.blank?
end
end

def options_with_glossary(options, from, to)
glossary = find_glossary(from, to)
glossary ? { glossary_id: glossary.id }.merge(options) : options
end

def all_ready_glossaries
@all_ready_glossaries ||= DeepL.glossaries.list
end

def find_glossary(from, to)
config_glossary_ids = @i18n_tasks.translation_config[:deepl_glossary_ids]
return unless config_glossary_ids

all_ready_glossaries.find do |glossary|
glossary.ready \
&& glossary.source_lang == from \
&& glossary.target_lang == to \
&& config_glossary_ids.include?(glossary.id)
end
end
end
end
3 changes: 3 additions & 0 deletions templates/config/i18n-tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ search:
# deepl_api_key: "48E92789-57A3-466A-9959-1A1A1A1A1A1A"
# # deepl_host: "https://api.deepl.com"
# # deepl_version: "v2"
# # deepl_glossary_ids:
# # - f28106eb-0e06-489e-82c6-8215d6f95089
# # - 2c6415be-1852-4f54-9e1b-d800463496b4
# # add additional options to the DeepL.translate call: https://www.deepl.com/docs-api/translate-text/translate-text/
# deepl_options:
# formality: prefer_less
Expand Down