Class: DeepL::Client
- Inherits:
-
Object
- Object
- DeepL::Client
- Defined in:
- lib/deepl/client.rb
Overview
Client for the DeepL API. Every client owns its configuration and HTTP connection: two clients never share options, and nothing is read from environment variables.
client = DeepL::Client.new(ENV.fetch('DEEPL_AUTH_KEY'), app_info: { app_name: 'my-app',
app_version: '1.2.3' })
All methods may raise a DeepL::Exceptions::Error or a subclass.
Class Method Summary collapse
-
.free_account_auth_key?(auth_key) ⇒ Boolean
Returns true if
auth_keyis a DeepL API Free key (it ends with:fx).
Instance Method Summary collapse
-
#correct_text(text, target_lang = nil, **options) ⇒ DeepL::Resources::Text+
Corrects the grammar, spelling and punctuation of
text. -
#create_glossary(name, source_lang, target_lang, entries, **options) ⇒ DeepL::Resources::Glossary
Creates a v2 glossary for one language pair.
-
#create_glossary_from_csv(name, source_lang, target_lang, csv, **options) ⇒ DeepL::Resources::Glossary
Creates a v2 glossary from CSV content, for example a file downloaded from the DeepL website.
-
#create_multilingual_glossary(name, dictionaries, **options) ⇒ DeepL::Resources::MultilingualGlossary
Creates a multilingual glossary with one dictionary per language pair.
-
#create_multilingual_glossary_from_csv(name, source_lang, target_lang, csv, **options) ⇒ DeepL::Resources::MultilingualGlossary
Creates a multilingual glossary with one dictionary from CSV content.
-
#create_spoken_terms(name, term_lists, **options) ⇒ DeepL::Resources::SpokenTerms
Creates a spoken terms collection, which improves the transcription of the given terms.
-
#create_style_profile(name, description: nil, industry: nil, glossary_ids: nil, style_rule_ids: nil, translation_memory_ids: nil, glossary_priority_order_enabled: nil, **options) ⇒ DeepL::Resources::StyleProfile
Creates a style profile, which links glossaries, style rule lists and translation memories.
-
#create_style_rule(name, language, configured_rules: nil, custom_instructions: nil, **options) ⇒ DeepL::Resources::StyleRule
Creates a style rule list.
-
#create_style_rule_custom_instruction(style_rule, label, prompt, source_language: nil, **options) ⇒ DeepL::Resources::CustomInstruction
Adds a custom instruction to a style rule list.
-
#create_translation_memory_export(translation_memory) ⇒ DeepL::Resources::TranslationMemoryExport
Creates an export job for a translation memory.
-
#create_translation_memory_import(file_name, content_length, content_type: nil, display_name: nil, extra_body_parameters: nil) ⇒ DeepL::Resources::TranslationMemoryImport
Creates an import job for a new translation memory.
-
#delete_glossary(glossary) ⇒ nil
Deletes a v2 glossary.
-
#delete_multilingual_glossary(glossary) ⇒ nil
Deletes a multilingual glossary.
-
#delete_multilingual_glossary_dictionary(glossary, source_lang, target_lang) ⇒ nil
Deletes the dictionary of one language pair from a multilingual glossary.
-
#delete_spoken_terms(spoken_terms) ⇒ nil
Deletes a spoken terms collection.
-
#delete_spoken_terms_list(spoken_terms, lang) ⇒ nil
Deletes the term list of one language of a collection.
-
#delete_style_profile(style_profile) ⇒ nil
Deletes a style profile.
-
#delete_style_rule(style_rule) ⇒ nil
Deletes a style rule list.
-
#delete_style_rule_custom_instruction(style_rule, instruction_id) ⇒ nil
Deletes a custom instruction of a style rule list.
-
#delete_translation_memory(translation_memory) ⇒ nil
Deletes a translation memory.
-
#download_translation_memory_export(job, output_path) ⇒ nil
Downloads the TMX file of a completed export job to
output_path. -
#export_translation_memory_to_filepath(translation_memory, output_path, timeout_s: nil) ⇒ DeepL::Resources::TranslationMemoryJob
Exports a translation memory to a TMX file: creates the export job, waits until it is completed and writes the file to
output_path. -
#get_all_style_rules(page: nil, page_size: nil, detailed: nil) ⇒ Array<DeepL::Resources::StyleRule>
Lists style rule lists, one page at a time.
-
#get_glossary(glossary) ⇒ DeepL::Resources::Glossary
Retrieves a v2 glossary, without its entries.
-
#get_glossary_entries(glossary) ⇒ Hash{String => String}
Retrieves the entries of a v2 glossary.
-
#get_glossary_languages ⇒ Array<DeepL::Resources::LanguagePair>
Retrieves the language pairs supported by glossaries: every source language combined with every other target language of
/v3/languages?resource=glossary. -
#get_language_resources ⇒ Array<DeepL::Resources::LanguageResource>
Retrieves the resources supported by the DeepL API with their features, and whether each feature requires support from the source and/or target language.
-
#get_languages_for_resource(resource, include: nil) ⇒ Array<DeepL::Resources::LanguageSupport>
Retrieves the languages supported by
resource, each with its per-feature support. -
#get_multilingual_glossary(glossary) ⇒ DeepL::Resources::MultilingualGlossary
Retrieves a multilingual glossary, without its entries.
-
#get_multilingual_glossary_dictionary_entries(glossary, source_lang, target_lang) ⇒ DeepL::Resources::MultilingualGlossaryDictionaryEntries
Retrieves the entries of the dictionary of one language pair.
-
#get_source_languages ⇒ Array<DeepL::Resources::Language>
Retrieves the languages that can be translated from, with upper-case codes (+EN+,
DE). -
#get_spoken_terms(spoken_terms) ⇒ DeepL::Resources::SpokenTerms
Retrieves a spoken terms collection.
-
#get_spoken_terms_entries(spoken_terms, lang) ⇒ DeepL::Resources::SpokenTermsEntries
Retrieves the terms of one language of a collection.
-
#get_style_profile(style_profile) ⇒ DeepL::Resources::StyleProfile
Retrieves a style profile.
-
#get_style_rule(style_rule) ⇒ DeepL::Resources::StyleRule
Retrieves a style rule list, including its configured rules and custom instructions.
-
#get_style_rule_custom_instruction(style_rule, instruction_id) ⇒ DeepL::Resources::CustomInstruction
Retrieves a custom instruction of a style rule list.
-
#get_target_languages ⇒ Array<DeepL::Resources::Language>
Retrieves the languages that can be translated into, with upper-case codes (+EN-GB+,
DE). -
#get_translation_memory(translation_memory) ⇒ DeepL::Resources::TranslationMemory
Retrieves a translation memory.
-
#get_translation_memory_job(job) ⇒ DeepL::Resources::TranslationMemoryJob
Retrieves the status of a translation memory import or export job once.
-
#get_usage ⇒ DeepL::Resources::Usage
Retrieves the usage and limits of the account and, for API Pro, of this API key.
-
#import_translation_memory_from_filepath(input_path, display_name: nil, content_type: nil, timeout_s: nil, extra_body_parameters: nil) ⇒ DeepL::Resources::TranslationMemoryJob
Imports a TMX file as a new translation memory: creates the import job, uploads the file and waits until the job is completed.
-
#initialize(auth_key, server_url: nil, headers: {}, max_retries: 5, timeout: 10, proxy: nil, send_platform_info: true, app_info: nil, http_client_options: nil, logger: nil) ⇒ Client
constructor
A keyword argument given as
niltakes its default, like an omitted one. - #inspect ⇒ Object
-
#list_glossaries ⇒ Array<DeepL::Resources::Glossary>
Lists all v2 glossaries.
-
#list_multilingual_glossaries ⇒ Array<DeepL::Resources::MultilingualGlossary>
Lists all multilingual glossaries.
-
#list_spoken_terms ⇒ Array<DeepL::Resources::SpokenTerms>
Lists all spoken terms collections.
-
#list_style_profiles(page: nil, page_size: nil) ⇒ Array<DeepL::Resources::StyleProfile>
Lists style profiles, one page at a time.
-
#list_translation_memories(page: nil, page_size: nil) ⇒ Array<DeepL::Resources::TranslationMemory>
Lists translation memories, one page at a time.
-
#list_translation_memory_segments(translation_memory, page_size: nil, page_cursor: nil, filter_text: nil, filter_case_sensitive: nil) ⇒ DeepL::Resources::TranslationMemorySegments
Retrieves one page of the segments of a translation memory.
-
#rephrase_text(text, target_lang = nil, writing_style: nil, tone: nil, **options) ⇒ DeepL::Resources::Text+
Rephrases
text, optionally into another variant or language and with a writing style or tone. -
#replace_multilingual_glossary_dictionary(glossary, dictionary, **options) ⇒ DeepL::Resources::MultilingualGlossaryDictionary
Replaces the entries of the dictionary of one language pair, or adds the dictionary if the language pair is new.
-
#replace_multilingual_glossary_dictionary_from_csv(glossary, source_lang, target_lang, csv, **options) ⇒ DeepL::Resources::MultilingualGlossaryDictionary
Replaces the entries of the dictionary of one language pair with CSV content.
-
#replace_spoken_terms_list(spoken_terms, entries, **options) ⇒ DeepL::Resources::SpokenTermsList
Replaces the terms of one language, or adds the list if the language is new.
-
#translate_document(input_path, output_path, source_lang, target_lang, timeout_s: nil, **options) ⇒ DeepL::Resources::DocumentTranslationStatus
Translates the document at
input_pathand writes the result tooutput_path: uploads it, waits until the translation is done and downloads it. -
#translate_document_download(handle, output_path) ⇒ nil
Downloads the translated document to
output_path. -
#translate_document_get_status(handle) ⇒ DeepL::Resources::DocumentTranslationStatus
Queries the status of a document translation once.
-
#translate_document_upload(input_path, source_lang, target_lang, **options) ⇒ DeepL::Resources::DocumentHandle
Uploads the document at
input_pathfor translation. -
#translate_document_wait_until_done(handle, timeout_s: nil) ⇒ DeepL::Resources::DocumentTranslationStatus
Polls the status of a document translation every 5 seconds until it is done.
-
#translate_text(text, source_lang, target_lang, **options) ⇒ DeepL::Resources::Text+
Translates
textfromsource_langintotarget_lang. -
#update_multilingual_glossary(glossary, name: nil, dictionaries: nil, **options) ⇒ DeepL::Resources::MultilingualGlossary
Renames a glossary and/or merges entries into its dictionaries.
-
#update_multilingual_glossary_dictionary(glossary, dictionary, **options) ⇒ DeepL::Resources::MultilingualGlossary
Merges entries into the dictionary of one language pair, or adds the dictionary if the language pair is new.
-
#update_multilingual_glossary_dictionary_from_csv(glossary, source_lang, target_lang, csv, **options) ⇒ DeepL::Resources::MultilingualGlossary
Merges CSV entries into the dictionary of one language pair, or adds the dictionary if the language pair is new.
-
#update_multilingual_glossary_name(glossary, name, **options) ⇒ DeepL::Resources::MultilingualGlossary
Renames a multilingual glossary.
-
#update_spoken_terms(spoken_terms, name: nil, term_lists: nil, **options) ⇒ DeepL::Resources::SpokenTerms
Renames a collection and/or merges terms into its lists.
-
#update_style_profile(style_profile, name: nil, description: nil, industry: nil, glossary_ids: nil, style_rule_ids: nil, translation_memory_ids: nil, glossary_priority_order_enabled: nil, **options) ⇒ DeepL::Resources::StyleProfile
Updates a style profile.
-
#update_style_rule_configured_rules(style_rule, configured_rules, **options) ⇒ DeepL::Resources::StyleRule
Replaces the configured rules of a style rule list.
-
#update_style_rule_custom_instruction(style_rule, instruction_id, label, prompt, source_language: nil, **options) ⇒ DeepL::Resources::CustomInstruction
Replaces a custom instruction of a style rule list.
-
#update_style_rule_name(style_rule, name, **options) ⇒ DeepL::Resources::StyleRule
Renames a style rule list.
-
#upload_translation_memory_file(translation_memory_import, content, content_type: nil) ⇒ nil
Uploads a TMX file to the upload URL of an import job, which starts the processing.
-
#wait_until_translation_memory_job_done(job, timeout_s: nil) ⇒ DeepL::Resources::TranslationMemoryJob
Polls a translation memory import or export job every 5 seconds until it is completed.
Constructor Details
#initialize(auth_key, server_url: nil, headers: {}, max_retries: 5, timeout: 10, proxy: nil, send_platform_info: true, app_info: nil, http_client_options: nil, logger: nil) ⇒ Client
A keyword argument given as nil takes its default, like an omitted one.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/deepl/client.rb', line 65 def initialize(auth_key, server_url: nil, headers: {}, max_retries: 5, timeout: 10, # rubocop:disable Metrics/ParameterLists proxy: nil, send_platform_info: true, app_info: nil, http_client_options: nil, logger: nil) free = self.class.free_account_auth_key?(auth_key) default_server_url = free ? DEEPL_SERVER_URL_FREE : DEEPL_SERVER_URL @api = Utils::ClientBuilder.build_api( auth_key, key_name: 'auth_key', server_url: server_url, default_server_url: default_server_url, headers: headers, max_retries: max_retries, timeout: timeout, proxy: proxy, send_platform_info: send_platform_info, app_info: app_info, http_client_options: , logger: logger ) end |
Class Method Details
.free_account_auth_key?(auth_key) ⇒ Boolean
Returns true if auth_key is a DeepL API Free key (it ends with :fx).
25 26 27 |
# File 'lib/deepl/client.rb', line 25 def self.free_account_auth_key?(auth_key) auth_key.to_s.strip.end_with?(':fx') end |
Instance Method Details
#correct_text(text, target_lang = nil, **options) ⇒ DeepL::Resources::Text+
Corrects the grammar, spelling and punctuation of text. Options set to nil are not
sent.
274 275 276 |
# File 'lib/deepl/client.rb', line 274 def correct_text(text, target_lang = nil, **) Requests::Correct.new(api, text, target_lang, ).request end |
#create_glossary(name, source_lang, target_lang, entries, **options) ⇒ DeepL::Resources::Glossary
Creates a v2 glossary for one language pair. Glossaries are not regional, so a regional
variant is sent as its base language (+EN-GB+ as EN).
Creation is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second glossary.
349 350 351 352 353 354 355 |
# File 'lib/deepl/client.rb', line 349 def create_glossary(name, source_lang, target_lang, entries, **) raise ArgumentError, 'Glossary entries must not be empty' if entries.nil? || entries.empty? tsv = Utils::GlossaryEntries.to_tsv(entries) Requests::Glossary::Create.new(api, name, source_lang, target_lang, tsv, 'tsv', ).request end |
#create_glossary_from_csv(name, source_lang, target_lang, csv, **options) ⇒ DeepL::Resources::Glossary
Creates a v2 glossary from CSV content, for example a file downloaded from the DeepL website.
Creation is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second glossary.
373 374 375 376 377 378 |
# File 'lib/deepl/client.rb', line 373 def create_glossary_from_csv(name, source_lang, target_lang, csv, **) csv = csv.read if csv.respond_to?(:read) Requests::Glossary::Create.new(api, name, source_lang, target_lang, csv, 'csv', ).request end |
#create_multilingual_glossary(name, dictionaries, **options) ⇒ DeepL::Resources::MultilingualGlossary
Creates a multilingual glossary with one dictionary per language pair.
Creation is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second glossary.
449 450 451 452 |
# File 'lib/deepl/client.rb', line 449 def create_multilingual_glossary(name, dictionaries, **) check_csv_entries!(dictionaries) Requests::MultilingualGlossary::Create.new(api, name, dictionaries, ).request end |
#create_multilingual_glossary_from_csv(name, source_lang, target_lang, csv, **options) ⇒ DeepL::Resources::MultilingualGlossary
Creates a multilingual glossary with one dictionary from CSV content.
Creation is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second glossary.
470 471 472 473 |
# File 'lib/deepl/client.rb', line 470 def create_multilingual_glossary_from_csv(name, source_lang, target_lang, csv, **) dictionary = csv_dictionary(source_lang, target_lang, csv) Requests::MultilingualGlossary::Create.new(api, name, [dictionary], ).request end |
#create_spoken_terms(name, term_lists, **options) ⇒ DeepL::Resources::SpokenTerms
Creates a spoken terms collection, which improves the transcription of the given terms.
Creation is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second collection.
671 672 673 |
# File 'lib/deepl/client.rb', line 671 def create_spoken_terms(name, term_lists, **) Requests::SpokenTerms::Create.new(api, name, term_lists, ).request end |
#create_style_profile(name, description: nil, industry: nil, glossary_ids: nil, style_rule_ids: nil, translation_memory_ids: nil, glossary_priority_order_enabled: nil, **options) ⇒ DeepL::Resources::StyleProfile
Creates a style profile, which links glossaries, style rule lists and translation
memories. Keywords left nil are not sent. If a component cannot be used, the request
fails and no style profile is created.
Creation is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second style profile.
950 951 952 953 954 955 956 957 |
# File 'lib/deepl/client.rb', line 950 def create_style_profile(name, description: nil, industry: nil, glossary_ids: nil, # rubocop:disable Metrics/ParameterLists style_rule_ids: nil, translation_memory_ids: nil, glossary_priority_order_enabled: nil, **) fields = { description: description, industry: industry, glossary_ids: glossary_ids, style_rule_ids: style_rule_ids, translation_memory_ids: translation_memory_ids, glossary_priority_order_enabled: glossary_priority_order_enabled } Requests::StyleProfile::Create.new(api, name, fields, ).request end |
#create_style_rule(name, language, configured_rules: nil, custom_instructions: nil, **options) ⇒ DeepL::Resources::StyleRule
Creates a style rule list. Keywords left nil are not sent.
Creation is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second style rule list.
792 793 794 795 796 797 |
# File 'lib/deepl/client.rb', line 792 def create_style_rule(name, language, configured_rules: nil, custom_instructions: nil, **) = .merge(configured_rules: configured_rules, custom_instructions: custom_instructions).compact Requests::StyleRule::Create.new(api, name, language, ).request end |
#create_style_rule_custom_instruction(style_rule, label, prompt, source_language: nil, **options) ⇒ DeepL::Resources::CustomInstruction
Adds a custom instruction to a style rule list. source_language is not sent if nil.
Creation is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second custom instruction.
866 867 868 869 870 |
# File 'lib/deepl/client.rb', line 866 def create_style_rule_custom_instruction(style_rule, label, prompt, source_language: nil, **) Requests::StyleRule::CreateCustomInstruction.new(api, style_rule, label, prompt, source_language, ).request end |
#create_translation_memory_export(translation_memory) ⇒ DeepL::Resources::TranslationMemoryExport
Creates an export job for a translation memory. Wait for it with #wait_until_translation_memory_job_done, then download the TMX file with #download_translation_memory_export.
Creation is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second export job.
1154 1155 1156 |
# File 'lib/deepl/client.rb', line 1154 def create_translation_memory_export(translation_memory) translation_memories.create_export(translation_memory) end |
#create_translation_memory_import(file_name, content_length, content_type: nil, display_name: nil, extra_body_parameters: nil) ⇒ DeepL::Resources::TranslationMemoryImport
Creates an import job for a new translation memory. Upload the TMX file to the returned
upload URL with #upload_translation_memory_file, then wait for the job with
#wait_until_translation_memory_job_done. Keywords left nil are not sent.
Creation is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second import job.
1078 1079 1080 1081 1082 1083 |
# File 'lib/deepl/client.rb', line 1078 def create_translation_memory_import(file_name, content_length, content_type: nil, display_name: nil, extra_body_parameters: nil) translation_memories.create_import(file_name, content_length, content_type: content_type, display_name: display_name, extra_body_parameters: extra_body_parameters) end |
#delete_glossary(glossary) ⇒ nil
Deletes a v2 glossary.
414 415 416 |
# File 'lib/deepl/client.rb', line 414 def delete_glossary(glossary) Requests::Glossary::Destroy.new(api, glossary).request end |
#delete_multilingual_glossary(glossary) ⇒ nil
Deletes a multilingual glossary.
651 652 653 |
# File 'lib/deepl/client.rb', line 651 def delete_multilingual_glossary(glossary) Requests::MultilingualGlossary::Destroy.new(api, glossary).request end |
#delete_multilingual_glossary_dictionary(glossary, source_lang, target_lang) ⇒ nil
Deletes the dictionary of one language pair from a multilingual glossary.
639 640 641 642 |
# File 'lib/deepl/client.rb', line 639 def delete_multilingual_glossary_dictionary(glossary, source_lang, target_lang) Requests::MultilingualGlossary::DestroyDictionary.new(api, glossary, source_lang, target_lang).request end |
#delete_spoken_terms(spoken_terms) ⇒ nil
Deletes a spoken terms collection.
718 719 720 |
# File 'lib/deepl/client.rb', line 718 def delete_spoken_terms(spoken_terms) Requests::SpokenTerms::Destroy.new(api, spoken_terms).request end |
#delete_spoken_terms_list(spoken_terms, lang) ⇒ nil
Deletes the term list of one language of a collection.
758 759 760 |
# File 'lib/deepl/client.rb', line 758 def delete_spoken_terms_list(spoken_terms, lang) Requests::SpokenTerms::DestroyTermList.new(api, spoken_terms, lang).request end |
#delete_style_profile(style_profile) ⇒ nil
Deletes a style profile.
998 999 1000 1001 |
# File 'lib/deepl/client.rb', line 998 def delete_style_profile(style_profile) Requests::StyleProfile::Destroy.new(api, style_profile).request nil end |
#delete_style_rule(style_rule) ⇒ nil
Deletes a style rule list.
845 846 847 848 |
# File 'lib/deepl/client.rb', line 845 def delete_style_rule(style_rule) Requests::StyleRule::Destroy.new(api, style_rule).request nil end |
#delete_style_rule_custom_instruction(style_rule, instruction_id) ⇒ nil
Deletes a custom instruction of a style rule list.
913 914 915 916 |
# File 'lib/deepl/client.rb', line 913 def delete_style_rule_custom_instruction(style_rule, instruction_id) Requests::StyleRule::DestroyCustomInstruction.new(api, style_rule, instruction_id).request nil end |
#delete_translation_memory(translation_memory) ⇒ nil
Deletes a translation memory.
1032 1033 1034 1035 |
# File 'lib/deepl/client.rb', line 1032 def delete_translation_memory(translation_memory) translation_memories.destroy(translation_memory) nil end |
#download_translation_memory_export(job, output_path) ⇒ nil
Downloads the TMX file of a completed export job to output_path. The download URL is a
pre-signed storage URL: no Authorization header and no configured headers are sent to
it.
1173 1174 1175 1176 |
# File 'lib/deepl/client.rb', line 1173 def download_translation_memory_export(job, output_path) translation_memories.download_export(job, output_path) nil end |
#export_translation_memory_to_filepath(translation_memory, output_path, timeout_s: nil) ⇒ DeepL::Resources::TranslationMemoryJob
Exports a translation memory to a TMX file: creates the export job, waits until it is
completed and writes the file to output_path.
Starting the export is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second export job.
1200 1201 1202 1203 |
# File 'lib/deepl/client.rb', line 1200 def export_translation_memory_to_filepath(translation_memory, output_path, timeout_s: nil) translation_memories.export_to_filepath(translation_memory, output_path, timeout_s: timeout_s) end |
#get_all_style_rules(page: nil, page_size: nil, detailed: nil) ⇒ Array<DeepL::Resources::StyleRule>
Lists style rule lists, one page at a time. Arguments left nil are not sent.
770 771 772 773 |
# File 'lib/deepl/client.rb', line 770 def get_all_style_rules(page: nil, page_size: nil, detailed: nil) = { page: page, page_size: page_size, detailed: detailed }.compact Requests::StyleRule::List.new(api, ).request end |
#get_glossary(glossary) ⇒ DeepL::Resources::Glossary
Retrieves a v2 glossary, without its entries.
394 395 396 |
# File 'lib/deepl/client.rb', line 394 def get_glossary(glossary) Requests::Glossary::Find.new(api, glossary).request end |
#get_glossary_entries(glossary) ⇒ Hash{String => String}
Retrieves the entries of a v2 glossary.
404 405 406 |
# File 'lib/deepl/client.rb', line 404 def get_glossary_entries(glossary) Requests::Glossary::Entries.new(api, glossary).request end |
#get_glossary_languages ⇒ Array<DeepL::Resources::LanguagePair>
Retrieves the language pairs supported by glossaries: every source language combined with
every other target language of /v3/languages?resource=glossary.
423 424 425 |
# File 'lib/deepl/client.rb', line 423 def get_glossary_languages # rubocop:disable Naming/AccessorMethodName Requests::Glossary::LanguagePairs.new(api).request end |
#get_language_resources ⇒ Array<DeepL::Resources::LanguageResource>
Retrieves the resources supported by the DeepL API with their features, and whether each feature requires support from the source and/or target language.
301 302 303 |
# File 'lib/deepl/client.rb', line 301 def get_language_resources # rubocop:disable Naming/AccessorMethodName Requests::LanguageResources.new(api).request end |
#get_languages_for_resource(resource, include: nil) ⇒ Array<DeepL::Resources::LanguageSupport>
Retrieves the languages supported by resource, each with its per-feature support. The
language codes are returned as the API sends them.
317 318 319 |
# File 'lib/deepl/client.rb', line 317 def get_languages_for_resource(resource, include: nil) Requests::LanguagesForResource.new(api, resource, { include: include }.compact).request end |
#get_multilingual_glossary(glossary) ⇒ DeepL::Resources::MultilingualGlossary
Retrieves a multilingual glossary, without its entries.
491 492 493 |
# File 'lib/deepl/client.rb', line 491 def get_multilingual_glossary(glossary) Requests::MultilingualGlossary::Find.new(api, glossary).request end |
#get_multilingual_glossary_dictionary_entries(glossary, source_lang, target_lang) ⇒ DeepL::Resources::MultilingualGlossaryDictionaryEntries
Retrieves the entries of the dictionary of one language pair.
626 627 628 |
# File 'lib/deepl/client.rb', line 626 def get_multilingual_glossary_dictionary_entries(glossary, source_lang, target_lang) Requests::MultilingualGlossary::Entries.new(api, glossary, source_lang, target_lang).request end |
#get_source_languages ⇒ Array<DeepL::Resources::Language>
Retrieves the languages that can be translated from, with upper-case codes (+EN+, DE).
283 284 285 |
# File 'lib/deepl/client.rb', line 283 def get_source_languages # rubocop:disable Naming/AccessorMethodName Requests::Languages.new(api, :source).request end |
#get_spoken_terms(spoken_terms) ⇒ DeepL::Resources::SpokenTerms
Retrieves a spoken terms collection.
690 691 692 |
# File 'lib/deepl/client.rb', line 690 def get_spoken_terms(spoken_terms) Requests::SpokenTerms::Find.new(api, spoken_terms).request end |
#get_spoken_terms_entries(spoken_terms, lang) ⇒ DeepL::Resources::SpokenTermsEntries
Retrieves the terms of one language of a collection.
730 731 732 |
# File 'lib/deepl/client.rb', line 730 def get_spoken_terms_entries(spoken_terms, lang) Requests::SpokenTerms::Entries.new(api, spoken_terms, lang).request end |
#get_style_profile(style_profile) ⇒ DeepL::Resources::StyleProfile
Retrieves a style profile.
966 967 968 |
# File 'lib/deepl/client.rb', line 966 def get_style_profile(style_profile) Requests::StyleProfile::Find.new(api, style_profile).request end |
#get_style_rule(style_rule) ⇒ DeepL::Resources::StyleRule
Retrieves a style rule list, including its configured rules and custom instructions.
807 808 809 |
# File 'lib/deepl/client.rb', line 807 def get_style_rule(style_rule) Requests::StyleRule::Find.new(api, style_rule).request end |
#get_style_rule_custom_instruction(style_rule, instruction_id) ⇒ DeepL::Resources::CustomInstruction
Retrieves a custom instruction of a style rule list.
881 882 883 |
# File 'lib/deepl/client.rb', line 881 def get_style_rule_custom_instruction(style_rule, instruction_id) Requests::StyleRule::FindCustomInstruction.new(api, style_rule, instruction_id).request end |
#get_target_languages ⇒ Array<DeepL::Resources::Language>
Retrieves the languages that can be translated into, with upper-case codes (+EN-GB+, DE).
292 293 294 |
# File 'lib/deepl/client.rb', line 292 def get_target_languages # rubocop:disable Naming/AccessorMethodName Requests::Languages.new(api, :target).request end |
#get_translation_memory(translation_memory) ⇒ DeepL::Resources::TranslationMemory
Retrieves a translation memory.
1021 1022 1023 |
# File 'lib/deepl/client.rb', line 1021 def get_translation_memory(translation_memory) translation_memories.find(translation_memory) end |
#get_translation_memory_job(job) ⇒ DeepL::Resources::TranslationMemoryJob
Retrieves the status of a translation memory import or export job once.
1211 1212 1213 |
# File 'lib/deepl/client.rb', line 1211 def get_translation_memory_job(job) translation_memories.find_job(job) end |
#get_usage ⇒ DeepL::Resources::Usage
Retrieves the usage and limits of the account and, for API Pro, of this API key.
325 326 327 |
# File 'lib/deepl/client.rb', line 325 def get_usage # rubocop:disable Naming/AccessorMethodName Requests::Usage.new(api).request end |
#import_translation_memory_from_filepath(input_path, display_name: nil, content_type: nil, timeout_s: nil, extra_body_parameters: nil) ⇒ DeepL::Resources::TranslationMemoryJob
Imports a TMX file as a new translation memory: creates the import job, uploads the file and waits until the job is completed.
Creating the import job is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second import job.
1131 1132 1133 1134 1135 1136 1137 |
# File 'lib/deepl/client.rb', line 1131 def import_translation_memory_from_filepath(input_path, display_name: nil, content_type: nil, timeout_s: nil, extra_body_parameters: nil) translation_memories.import_from_filepath( input_path, display_name: display_name, content_type: content_type, timeout_s: timeout_s, extra_body_parameters: extra_body_parameters ) end |
#inspect ⇒ Object
1230 1231 1232 |
# File 'lib/deepl/client.rb', line 1230 def inspect "#<#{self.class.name} server_url=#{configuration.host}>" end |
#list_glossaries ⇒ Array<DeepL::Resources::Glossary>
Lists all v2 glossaries.
384 385 386 |
# File 'lib/deepl/client.rb', line 384 def list_glossaries Requests::Glossary::List.new(api).request end |
#list_multilingual_glossaries ⇒ Array<DeepL::Resources::MultilingualGlossary>
Lists all multilingual glossaries.
479 480 481 |
# File 'lib/deepl/client.rb', line 479 def list_multilingual_glossaries Requests::MultilingualGlossary::List.new(api).request end |
#list_spoken_terms ⇒ Array<DeepL::Resources::SpokenTerms>
Lists all spoken terms collections.
679 680 681 |
# File 'lib/deepl/client.rb', line 679 def list_spoken_terms Requests::SpokenTerms::List.new(api).request end |
#list_style_profiles(page: nil, page_size: nil) ⇒ Array<DeepL::Resources::StyleProfile>
Lists style profiles, one page at a time. Arguments left nil are not sent.
924 925 926 |
# File 'lib/deepl/client.rb', line 924 def list_style_profiles(page: nil, page_size: nil) Requests::StyleProfile::List.new(api, { page: page, page_size: page_size }.compact).request end |
#list_translation_memories(page: nil, page_size: nil) ⇒ Array<DeepL::Resources::TranslationMemory>
Lists translation memories, one page at a time. Arguments left nil are not sent.
1009 1010 1011 |
# File 'lib/deepl/client.rb', line 1009 def list_translation_memories(page: nil, page_size: nil) translation_memories.list({ page: page, page_size: page_size }.compact) end |
#list_translation_memory_segments(translation_memory, page_size: nil, page_cursor: nil, filter_text: nil, filter_case_sensitive: nil) ⇒ DeepL::Resources::TranslationMemorySegments
Retrieves one page of the segments of a translation memory. Pagination is cursor-based:
omit page_cursor on the first call, then pass the next_page_cursor of the previous page
until it is nil. Arguments left nil are not sent.
1052 1053 1054 1055 1056 1057 |
# File 'lib/deepl/client.rb', line 1052 def list_translation_memory_segments(translation_memory, page_size: nil, page_cursor: nil, filter_text: nil, filter_case_sensitive: nil) = { page_size: page_size, page_cursor: page_cursor, filter_text: filter_text, filter_case_sensitive: filter_case_sensitive }.compact translation_memories.segments(translation_memory, ) end |
#rephrase_text(text, target_lang = nil, writing_style: nil, tone: nil, **options) ⇒ DeepL::Resources::Text+
Rephrases text, optionally into another variant or language and with a writing style
or tone. Options set to nil are not sent.
257 258 259 |
# File 'lib/deepl/client.rb', line 257 def rephrase_text(text, target_lang = nil, writing_style: nil, tone: nil, **) Requests::Rephrase.new(api, text, target_lang, writing_style, tone, ).request end |
#replace_multilingual_glossary_dictionary(glossary, dictionary, **options) ⇒ DeepL::Resources::MultilingualGlossaryDictionary
Replaces the entries of the dictionary of one language pair, or adds the dictionary if the language pair is new.
587 588 589 590 591 |
# File 'lib/deepl/client.rb', line 587 def replace_multilingual_glossary_dictionary(glossary, dictionary, **) check_csv_entries!([dictionary]) Requests::MultilingualGlossary::ReplaceDictionary.new(api, glossary, dictionary, ).request end |
#replace_multilingual_glossary_dictionary_from_csv(glossary, source_lang, target_lang, csv, **options) ⇒ DeepL::Resources::MultilingualGlossaryDictionary
Replaces the entries of the dictionary of one language pair with CSV content.
606 607 608 609 610 611 |
# File 'lib/deepl/client.rb', line 606 def replace_multilingual_glossary_dictionary_from_csv(glossary, source_lang, target_lang, csv, **) dictionary = non_empty_csv_dictionary(source_lang, target_lang, csv) Requests::MultilingualGlossary::ReplaceDictionary.new(api, glossary, dictionary, ).request end |
#replace_spoken_terms_list(spoken_terms, entries, **options) ⇒ DeepL::Resources::SpokenTermsList
Replaces the terms of one language, or adds the list if the language is new.
746 747 748 |
# File 'lib/deepl/client.rb', line 746 def replace_spoken_terms_list(spoken_terms, entries, **) Requests::SpokenTerms::ReplaceTermList.new(api, spoken_terms, entries, ).request end |
#translate_document(input_path, output_path, source_lang, target_lang, timeout_s: nil, **options) ⇒ DeepL::Resources::DocumentTranslationStatus
Translates the document at input_path and writes the result to output_path: uploads
it, waits until the translation is done and downloads it.
The upload is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second document.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/deepl/client.rb', line 139 def translate_document(input_path, output_path, source_lang, target_lang, timeout_s: nil, # rubocop:disable Metrics/MethodLength **) raise ArgumentError, "Input file does not exist: #{input_path}" unless File.exist?(input_path) check_document_output_path(output_path) handle = nil begin handle = translate_document_upload(input_path, source_lang, target_lang, **) status = translate_document_wait_until_done(handle, timeout_s: timeout_s) translate_document_download(handle, output_path) status rescue StandardError => e FileUtils.rm_f(output_path) raise Exceptions::DocumentTranslationError.new( "Error occurred during document translation: #{e.}", handle, e ) end end |
#translate_document_download(handle, output_path) ⇒ nil
Downloads the translated document to output_path.
234 235 236 237 238 239 |
# File 'lib/deepl/client.rb', line 234 def translate_document_download(handle, output_path) Utils::OutputPath.check!(output_path) Requests::Document::Download.new(api, handle.document_id, handle.document_key, output_path).request nil end |
#translate_document_get_status(handle) ⇒ DeepL::Resources::DocumentTranslationStatus
Queries the status of a document translation once.
207 208 209 |
# File 'lib/deepl/client.rb', line 207 def translate_document_get_status(handle) Requests::Document::GetStatus.new(api, handle.document_id, handle.document_key).request end |
#translate_document_upload(input_path, source_lang, target_lang, **options) ⇒ DeepL::Resources::DocumentHandle
Uploads the document at input_path for translation. Options set to nil are not sent.
The upload is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second document.
193 194 195 196 197 198 199 200 |
# File 'lib/deepl/client.rb', line 193 def translate_document_upload(input_path, source_lang, target_lang, **) raise ArgumentError, "Input file does not exist: #{input_path}" unless File.exist?(input_path) raise Errno::EISDIR, input_path if File.directory?(input_path) filename = .delete(:filename) Requests::Document::Upload.new(api, input_path, source_lang, target_lang, filename, ).request end |
#translate_document_wait_until_done(handle, timeout_s: nil) ⇒ DeepL::Resources::DocumentTranslationStatus
Polls the status of a document translation every 5 seconds until it is done.
220 221 222 |
# File 'lib/deepl/client.rb', line 220 def translate_document_wait_until_done(handle, timeout_s: nil) Utils::DocumentPoller.wait_until_done(api, handle, timeout_s: timeout_s) end |
#translate_text(text, source_lang, target_lang, **options) ⇒ DeepL::Resources::Text+
Translates text from source_lang into target_lang. Always requests the billed
characters; options set to nil are not sent.
110 111 112 |
# File 'lib/deepl/client.rb', line 110 def translate_text(text, source_lang, target_lang, **) Requests::Translate.new(api, text, source_lang, target_lang, ).request end |
#update_multilingual_glossary(glossary, name: nil, dictionaries: nil, **options) ⇒ DeepL::Resources::MultilingualGlossary
Renames a glossary and/or merges entries into its dictionaries. Arguments left nil are
not sent; without any, an empty object is sent.
511 512 513 514 515 |
# File 'lib/deepl/client.rb', line 511 def update_multilingual_glossary(glossary, name: nil, dictionaries: nil, **) check_csv_entries!(dictionaries) unless dictionaries.nil? Requests::MultilingualGlossary::Update.new(api, glossary, name, dictionaries, ).request end |
#update_multilingual_glossary_dictionary(glossary, dictionary, **options) ⇒ DeepL::Resources::MultilingualGlossary
Merges entries into the dictionary of one language pair, or adds the dictionary if the language pair is new.
546 547 548 549 550 |
# File 'lib/deepl/client.rb', line 546 def update_multilingual_glossary_dictionary(glossary, dictionary, **) check_csv_entries!([dictionary]) Requests::MultilingualGlossary::Update.new(api, glossary, nil, [dictionary], ).request end |
#update_multilingual_glossary_dictionary_from_csv(glossary, source_lang, target_lang, csv, **options) ⇒ DeepL::Resources::MultilingualGlossary
Merges CSV entries into the dictionary of one language pair, or adds the dictionary if the language pair is new.
566 567 568 569 570 571 |
# File 'lib/deepl/client.rb', line 566 def update_multilingual_glossary_dictionary_from_csv(glossary, source_lang, target_lang, csv, **) dictionary = non_empty_csv_dictionary(source_lang, target_lang, csv) Requests::MultilingualGlossary::Update.new(api, glossary, nil, [dictionary], ).request end |
#update_multilingual_glossary_name(glossary, name, **options) ⇒ DeepL::Resources::MultilingualGlossary
Renames a multilingual glossary.
526 527 528 529 530 |
# File 'lib/deepl/client.rb', line 526 def update_multilingual_glossary_name(glossary, name, **) raise ArgumentError, 'Glossary name must not be empty' if name.to_s.empty? Requests::MultilingualGlossary::Update.new(api, glossary, name, nil, ).request end |
#update_spoken_terms(spoken_terms, name: nil, term_lists: nil, **options) ⇒ DeepL::Resources::SpokenTerms
Renames a collection and/or merges terms into its lists. Arguments left nil are not
sent; without any, an empty object is sent.
708 709 710 |
# File 'lib/deepl/client.rb', line 708 def update_spoken_terms(spoken_terms, name: nil, term_lists: nil, **) Requests::SpokenTerms::Update.new(api, spoken_terms, name, term_lists, ).request end |
#update_style_profile(style_profile, name: nil, description: nil, industry: nil, glossary_ids: nil, style_rule_ids: nil, translation_memory_ids: nil, glossary_priority_order_enabled: nil, **options) ⇒ DeepL::Resources::StyleProfile
Updates a style profile. Keywords left nil are not sent and leave the field unchanged;
without any, an empty object is sent. An empty string clears description or industry.
A component list replaces the linked list in full; an empty array unlinks all components
of that kind.
982 983 984 985 986 987 988 989 990 |
# File 'lib/deepl/client.rb', line 982 def update_style_profile(style_profile, name: nil, description: nil, industry: nil, # rubocop:disable Metrics/ParameterLists glossary_ids: nil, style_rule_ids: nil, translation_memory_ids: nil, glossary_priority_order_enabled: nil, **) fields = { name: name, description: description, industry: industry, glossary_ids: glossary_ids, style_rule_ids: style_rule_ids, translation_memory_ids: translation_memory_ids, glossary_priority_order_enabled: glossary_priority_order_enabled } Requests::StyleProfile::Update.new(api, style_profile, fields, ).request end |
#update_style_rule_configured_rules(style_rule, configured_rules, **options) ⇒ DeepL::Resources::StyleRule
Replaces the configured rules of a style rule list.
833 834 835 836 |
# File 'lib/deepl/client.rb', line 833 def update_style_rule_configured_rules(style_rule, configured_rules, **) Requests::StyleRule::UpdateConfiguredRules.new(api, style_rule, configured_rules, ).request end |
#update_style_rule_custom_instruction(style_rule, instruction_id, label, prompt, source_language: nil, **options) ⇒ DeepL::Resources::CustomInstruction
Replaces a custom instruction of a style rule list. source_language is not sent if
nil.
898 899 900 901 902 903 |
# File 'lib/deepl/client.rb', line 898 def update_style_rule_custom_instruction(style_rule, instruction_id, label, prompt, source_language: nil, **) Requests::StyleRule::UpdateCustomInstruction.new(api, style_rule, instruction_id, label, prompt, source_language, ).request end |
#update_style_rule_name(style_rule, name, **options) ⇒ DeepL::Resources::StyleRule
Renames a style rule list.
820 821 822 |
# File 'lib/deepl/client.rb', line 820 def update_style_rule_name(style_rule, name, **) Requests::StyleRule::Update.new(api, style_rule, name, ).request end |
#upload_translation_memory_file(translation_memory_import, content, content_type: nil) ⇒ nil
Uploads a TMX file to the upload URL of an import job, which starts the processing. The
upload URL is a pre-signed storage URL: no Authorization header and no configured
headers are sent to it.
1099 1100 1101 1102 |
# File 'lib/deepl/client.rb', line 1099 def upload_translation_memory_file(translation_memory_import, content, content_type: nil) translation_memories.upload_file(translation_memory_import, content, content_type: content_type) end |
#wait_until_translation_memory_job_done(job, timeout_s: nil) ⇒ DeepL::Resources::TranslationMemoryJob
Polls a translation memory import or export job every 5 seconds until it is completed.
1226 1227 1228 |
# File 'lib/deepl/client.rb', line 1226 def wait_until_translation_memory_job_done(job, timeout_s: nil) translation_memories.wait_until_job_done(job, timeout_s: timeout_s) end |