-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy path__main__.py
More file actions
61 lines (51 loc) · 1.74 KB
/
Copy path__main__.py
File metadata and controls
61 lines (51 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Copyright 2023 DeepL SE (https://www.deepl.com)
# Use of this source code is governed by an MIT
# license that can be found in the LICENSE file.
import io
import deepl
import os
env_auth_key = "DEEPL_AUTH_KEY"
env_server_url = "DEEPL_SERVER_URL"
def main() -> None:
auth_key = os.getenv(env_auth_key)
server_url = os.getenv(env_server_url)
if auth_key is None:
raise Exception(
f"Please provide authentication key via the {env_auth_key} "
"environment variable or --auth_key argument"
)
# Create a Translator object, and call get_usage() to validate connection
translator: deepl.Translator = deepl.Translator(
auth_key, server_url=server_url
)
u: deepl.Usage = translator.get_usage()
u.any_limit_exceeded
# Use most translation features of the library
_ = translator.translate_text(
["I am an example sentence", "I am another sentence"],
source_lang="EN",
target_lang="FR",
formality=deepl.Formality.DEFAULT,
tag_handling=None,
)
ginfo: deepl.GlossaryInfo = translator.create_glossary(
"Test Glossary", "DE", "FR", {"Hallo": "Bonjour"}
)
with io.BytesIO() as output_file:
doc_status: deepl.DocumentStatus = translator.translate_document(
"My example document",
output_file,
source_lang="DE",
target_lang="FR",
filename="example.txt",
glossary=ginfo,
)
doc_status.done
_ = translator.translate_text_with_glossary(
["Ich bin ein Beispielsatz.", "Ich bin noch ein Satz."], glossary=ginfo
)
print("Success")
# cleanup
translator.delete_glossary(ginfo.glossary_id)
if __name__ == "__main__":
main()