Penjabaran dari 9apps DeepL TranslateDeepL Translate is a free app that delivers fast, high-quality, and affordable language translation. DeepL translation is a form of language translation automation that uses deep learning models to deliver more accurate translation. Show deep-translator provides out of the box usage of proxies. Just define your proxies config as a dictionary and pass it to the corresponding translator. Below is an example using the GoogleTranslator, but this feature can be used with all supported translators. 9Deep-translator supports a series of command line arguments for quick and simple access to the translators directly in your console. DeepL Translate adalah aplikasi terjemahan andalan untuk menerjemahkan teks, ucapan, gambar, dan file dalam 29 bahasa. Setiap hari, jutaan orang menggunakannya untuk berkomunikasi dan mengatasi kendala bahasa. Mulai gunakan sekarang juga untuk mendapatkan terjemahan gratis, cepat, dan sangat akurat.* Terjemahkan teks dengan cepat antara 29 bahasa dengan mengetik: Belanda, Bulgaria, Ceko, Denmark, Estonia, Finlandia, Hongaria, Indonesia, Inggris (Amerika), Inggris (Inggris), Italia, Jepang, Jerman, Latvia, Lituania, Mandarin (sederhana), Polandia, Portugis, Portugis (Brasil), Prancis, Romania, Rusia, Slovenia, Slowakia, Spanyol, Swedia, Turki, Yunani, Ukraina Syarat & Ketentuan: [https://www.deepl.com/app-terms] Kebijakan Privasi: [https://www.deepl.com/privacy.html] Dukungan DeepL: [https://www.deepl.com/support] Pemberitahuan Izin: DeepL Translate dapat meminta izin untuk mengakses fitur-fitur berikut: The DeepL API is a language translation API that allows other computer programs to send texts and documents to DeepL's servers and receive high-quality translations. This opens a whole universe of opportunities for developers: any translation product you can imagine can now be built on top of DeepL's best-in-class translation technology. The DeepL Python library offers a convenient way for applications written in Python to interact with the DeepL API. We intend to support all API functions with the library, though support for new features may be added to the library after they’re added to the API. Getting an authentication keyTo use the DeepL Python Library, you'll need an API authentication key. To get a key, . With a DeepL API Free account you can translate up to 500,000 characters/month for free. InstallationThe library can be installed from PyPI using pip: pip install --upgrade deepl If you need to modify this source code, install the dependencies using poetry: poetry install On Ubuntu 22.04 an error might occur: import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"0. Use the workaround import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"1 as explained in this bug report. RequirementsThe library is tested with Python versions 3.6 to 3.11. The import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"2 module is used to perform HTTP requests; the minimum is version 2.0. UsageImport the package and construct a import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"3. The first argument is a string containing your API authentication key as found in your DeepL Pro Account. Be careful not to expose your key, for example when sharing source code. import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" This example is for demonstration purposes only. In production code, the authentication key should not be hard-coded, but instead fetched from a configuration file or environment variable. import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"3 accepts additional options, see for more information. Translating textTo translate text, call import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"5. The first argument is a string containing the text you want to translate, or a list of strings if you want to translate multiple texts. import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 and import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"7 specify the source and target language codes respectively. The import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 is optional, if it is unspecified the source language will be auto-detected. Language codes are case-insensitive strings according to ISO 639-1, for example import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"9, # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'0, # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'1. Some target languages also include the regional variant according to ISO 3166-1, for example # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'2, or # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'3. The full list of supported languages is in the API documentation. There are additional optional arguments to control translation, see below. import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"5 returns a # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'5, or a list of # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'5s corresponding to your input text(s). # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'5 has two properties: # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'8 is the translated text, and # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'9 is the detected source language code. # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?' Text translation optionsIn addition to the input text(s) argument, the available import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"5 arguments are:
The following options are only used if # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )2 is # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )4:
For a detailed explanation of the XML handling options, see the API documentation. Translating documentsTo translate documents, you may call either # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)7 using file IO objects, or # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)8 using file paths. For both functions, the first and second arguments correspond to the input and output files respectively. Just as for the import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"5 function, the import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 and import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"7 arguments specify the source and target language codes. There are additional optional arguments to control translation, see below. # Translate a formal document from English to German input_path = "/path/to/Instruction Manual.docx" output_path = "/path/to/Bedienungsanleitung.docx" try: # Using translate_document_from_filepath() with file paths translator.translate_document_from_filepath( input_path, output_path, target_lang="DE", formality="more" ) # Alternatively you can use translate_document() with file IO objects with open(input_path, "rb") as in_file, open(output_path, "wb") as out_file: translator.translate_document( in_file, out_file, target_lang="DE", formality="more" ) except deepl.DocumentTranslationException as error: # If an error occurs during document translation after the document was # already uploaded, a DocumentTranslationException is raised. The # document_handle property contains the document handle that may be used to # later retrieve the document from the server, or contact DeepL support. doc_id = error.document_handle.id doc_key = error.document_handle.key print(f"Error after uploading ${error}, id: ${doc_id} key: ${doc_key}") except deepl.DeepLException as error: # Errors during upload raise a DeepLException print(error) # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)7 and # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)8 are convenience functions that wrap multiple API calls: uploading, polling status until the translation is complete, and downloading. If your application needs to execute these steps individually, you can instead use the following functions directly:
Document translation optionsIn addition to the input file, output file, import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 and import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"7 arguments, the available # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)7 and # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)8 arguments are:
GlossariesGlossaries allow you to customize your translations using user-defined terms. Multiple glossaries can be stored with your account, each with a user-specified name and a uniquely-assigned ID. Creating a glossaryYou can create a glossary with your desired terms and name using text = "The artist was awarded a prize." with_glossary = translator.translate_text( text, source_lang="EN", target_lang="DE", glossary=my_glossary, ) print(with_glossary) # "Der Maler wurde mit einem Gewinn ausgezeichnet." # For comparison, the result without a glossary: without_glossary = translator.translate_text(text, target_lang="DE") print(without_glossary) # "Der Künstler wurde mit einem Preis ausgezeichnet."4. Each glossary applies to a single source-target language pair. Note: Glossaries are only supported for some language pairs, see for more information. The entries should be specified as a dictionary. If successful, the glossary is created and stored with your DeepL account, and a # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 object is returned including the ID, name, languages and entry count. # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries You can also upload a glossary downloaded from the DeepL website using text = "The artist was awarded a prize." with_glossary = translator.translate_text( text, source_lang="EN", target_lang="DE", glossary=my_glossary, ) print(with_glossary) # "Der Maler wurde mit einem Gewinn ausgezeichnet." # For comparison, the result without a glossary: without_glossary = translator.translate_text(text, target_lang="DE") print(without_glossary) # "Der Künstler wurde mit einem Preis ausgezeichnet."6. Instead of supplying the entries as a dictionary, specify the CSV data as text = "The artist was awarded a prize." with_glossary = translator.translate_text( text, source_lang="EN", target_lang="DE", glossary=my_glossary, ) print(with_glossary) # "Der Maler wurde mit einem Gewinn ausgezeichnet." # For comparison, the result without a glossary: without_glossary = translator.translate_text(text, target_lang="DE") print(without_glossary) # "Der Künstler wurde mit einem Preis ausgezeichnet."7 either as a file-like object or string or bytes containing file content: # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, ) The API documentation explains the expected CSV format in detail. Getting, listing and deleting stored glossariesFunctions to get, list, and delete stored glossaries are also provided:
# Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary) Listing entries in a stored glossaryThe # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 object does not contain the glossary entries, but instead only the number of entries in the poetry install05 property. To list the entries contained within a stored glossary, use poetry install06 providing either the # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 object or glossary ID: entries = translator.get_glossary_entries(my_glossary) print(entries) # "{'artist': 'Maler', 'prize': 'Gewinn'}" Using a stored glossaryYou can use a stored glossary for text translation by setting the # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries9 argument to either the glossary ID or # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 object. You must also specify the import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 argument (it is required when using a glossary): text = "The artist was awarded a prize." with_glossary = translator.translate_text( text, source_lang="EN", target_lang="DE", glossary=my_glossary, ) print(with_glossary) # "Der Maler wurde mit einem Gewinn ausgezeichnet." # For comparison, the result without a glossary: without_glossary = translator.translate_text(text, target_lang="DE") print(without_glossary) # "Der Künstler wurde mit einem Preis ausgezeichnet." Using a stored glossary for document translation is the same: set the # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries9 argument and specify the import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 argument: poetry install0 The # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)7, # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)8 and entries = translator.get_glossary_entries(my_glossary) print(entries) # "{'artist': 'Maler', 'prize': 'Gewinn'}"4 functions all support the # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries9 argument. Checking account usageTo check account usage, use the poetry install17 function. The returned poetry install18 object contains three usage subtypes: poetry install19, poetry install20 and poetry install21. Depending on your account type, some usage subtypes may be invalid; this can be checked using the poetry install22 property. For API accounts:
Each usage subtype (if valid) has poetry install28 and poetry install29 properties giving the amount used and maximum amount respectively, and the poetry install30 property that checks if the usage has reached the limit. The top level poetry install18 object has the poetry install32 property to check all usage subtypes. poetry install1 Listing available languagesYou can request the list of languages supported by DeepL for text and documents using the poetry install33 and poetry install34 functions. They both return a list of poetry install35 objects. The poetry install36 property gives the name of the language in English, and the poetry install37 property gives the language code. The poetry install38 property only appears for target languages, and indicates whether the target language supports the optional # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries4 parameter. poetry install2 Listing available glossary languagesGlossaries are supported for a subset of language pairs. To retrieve those languages use the poetry install40 function, which returns an array of poetry install41 objects. Each has import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 and import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"7 properties indicating that that pair of language codes is supported. poetry install3 You can also find the list of supported glossary language pairs in the API documentation. Note that glossaries work for all target regional-variants: a glossary for the target language English ( poetry install44) supports translations to both American English ( poetry install45) and British English ( poetry install46). ExceptionsAll module functions may raise poetry install47 or one of its subclasses. If invalid arguments are provided, they may raise the standard exceptions poetry install48 and poetry install49. ConfigurationLoggingLogging can be enabled to see the HTTP requests sent and responses received by the library. Enable and control logging using Python's poetry install50 module, for example: poetry install4 Server URL configurationYou can override the URL of the DeepL API by specifying the poetry install51 argument when constructing a poetry install52. This may be useful for testing purposes. You do not need to specify the URL to distinguish API Free and API Pro accounts, the library selects the correct URL automatically. poetry install5 Proxy configurationYou can configure a proxy by specifying the poetry install53 argument when constructing a poetry install52: poetry install6 The proxy argument is passed to the underlying import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"2 session, see the ; a dictionary of schemes to proxy URLs is also accepted. Command Line InterfaceThe library can be run on the command line supporting all API functions. Use the poetry install56 option for usage information: poetry install7 The CLI requires your DeepL authentication key specified either as the poetry install57 environment variable, or using the poetry install58 option, for example: poetry install8 Note that the poetry install58 argument must appear before the command argument. The recognized commands are:CommandDescriptiontexttranslate text(s)documenttranslate document(s)usageprint usage information for the current billing periodlanguagesprint available languagesglossarycreate, list, and remove glossaries For example, to translate text: poetry install9 Wrap text arguments in quotes to prevent the shell from splitting sentences into words. IssuesIf you experience problems using the library, or would like to request a new feature, please open an issue. DevelopmentWe welcome Pull Requests, please read the contributing guidelines. TestsExecute the tests using poetry install60. The tests communicate with the DeepL API using the auth key defined by the poetry install57 environment variable. Be aware that the tests make DeepL API requests that contribute toward your API usage. The test suite may instead be configured to communicate with the mock-server provided by deepl-mock. Although most test cases work for either, some test cases work only with the DeepL API or the mock-server and will be otherwise skipped. The test cases that require the mock-server trigger server errors and test the client error-handling. To execute the tests using deepl-mock, run it in another terminal while executing the tests. Execute the tests using poetry install60 with the poetry install63 and poetry install64 environment variables defined referring to the mock-server. Note: for testing on Python 3.6, you need to install an older version of poetry install60 manually, using poetry install66. |