diff --git a/README.md b/README.md index fd86f6b9..93e532cb 100644 --- a/README.md +++ b/README.md @@ -428,6 +428,11 @@ translation: deepl_api_key: deepl_host: deepl_version: + deepl_glossary_ids: + - f28106eb-0e06-489e-82c6-8215d6f95089 + - 2c6415be-1852-4f54-9e1b-d800463496b4 + deepl_options: + formality: prefer_less ``` or via environment variables: diff --git a/lib/i18n/tasks/translators/deepl_translator.rb b/lib/i18n/tasks/translators/deepl_translator.rb index 07a55bc5..fa4dc747 100644 --- a/lib/i18n/tasks/translators/deepl_translator.rb +++ b/lib/i18n/tasks/translators/deepl_translator.rb @@ -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 @@ -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 diff --git a/templates/config/i18n-tasks.yml b/templates/config/i18n-tasks.yml index 69116b64..e9962752 100644 --- a/templates/config/i18n-tasks.yml +++ b/templates/config/i18n-tasks.yml @@ -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