|
| 1 | +# Copyright 2023 DeepL SE (https://www.deepl.com) |
| 2 | +# Use of this source code is governed by an MIT |
| 3 | +# license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import io |
| 6 | +import deepl |
| 7 | +import os |
| 8 | + |
| 9 | +env_auth_key = "DEEPL_AUTH_KEY" |
| 10 | +env_server_url = "DEEPL_SERVER_URL" |
| 11 | + |
| 12 | + |
| 13 | +def main(): |
| 14 | + auth_key = os.getenv(env_auth_key) |
| 15 | + server_url = os.getenv(env_server_url) |
| 16 | + if auth_key is None: |
| 17 | + raise Exception( |
| 18 | + f"Please provide authentication key via the {env_auth_key} " |
| 19 | + "environment variable or --auth_key argument" |
| 20 | + ) |
| 21 | + |
| 22 | + # Create a Translator object, and call get_usage() to validate connection |
| 23 | + translator = deepl.Translator(auth_key, server_url=server_url) |
| 24 | + translator.get_usage() |
| 25 | + |
| 26 | + # Use most translation features of the library |
| 27 | + translator.translate_text( |
| 28 | + ["I am an example sentence", "I am another sentence"], |
| 29 | + source_lang="EN", |
| 30 | + target_lang="FR", |
| 31 | + formality=deepl.Formality.DEFAULT, |
| 32 | + tag_handling=None, |
| 33 | + ) |
| 34 | + ginfo = translator.create_glossary( |
| 35 | + "Test Glossary", "DE", "FR", {"Hallo": "Bonjour"} |
| 36 | + ) |
| 37 | + with io.BytesIO() as output_file: |
| 38 | + translator.translate_document( |
| 39 | + "My example document", |
| 40 | + output_file, |
| 41 | + source_lang="DE", |
| 42 | + target_lang="FR", |
| 43 | + filename="example.txt", |
| 44 | + glossary=ginfo, |
| 45 | + ) |
| 46 | + translator.translate_text_with_glossary( |
| 47 | + ["Ich bin ein Beispielsatz.", "Ich bin noch ein Satz."], glossary=ginfo |
| 48 | + ) |
| 49 | + |
| 50 | + print("Success") |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + main() |
0 commit comments