forked from OpenSlides/openslides-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
122 lines (103 loc) · 4 KB
/
translate.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import sys
from typing import Any
from datastore.reader.core import GetAllRequest, GetRequest, Reader
from datastore.reader.services import register_services as register_reader_services
from datastore.shared.di import injector
from datastore.shared.util import DeletedModelsBehaviour, fqid_from_collection_and_id
from datastore.writer.core import RequestUpdateEvent, Writer, WriteRequest
from datastore.writer.services import register_services as register_writer_services
from openslides_backend.i18n.translator import Translator
from openslides_backend.models.models import Organization
from openslides_backend.shared.util import ONE_ORGANIZATION_FQID
collection_to_fields_map = {
"organization": [
"name",
"login_text",
"description",
],
"meeting": [
"name",
"welcome_title",
"welcome_text",
"motion_preamble",
"motions_export_title",
"assignments_export_title",
"users_pdf_welcometitle",
"users_pdf_welcometext",
"users_email_sender",
"users_email_subject",
"users_email_body",
],
"group": ["name"],
"motion_workflow": ["name"],
"motion_state": ["name", "recommendation_label"],
"projector_countdown": ["name"],
"projector": ["name"],
"motion": ["recommendation_label"],
}
possible_languages = Organization().default_language.constraints["enum"]
def read_collection(collection: str, fields: list[str]) -> Any:
reader: Reader = injector.get(Reader)
with reader.get_database_context():
response = reader.get_all(
GetAllRequest(
collection, ["id", *fields], DeletedModelsBehaviour.NO_DELETED
)
)
return response.items()
def check_language(language: str) -> None:
if language not in possible_languages:
print("language is not allowed.")
print_help()
sys.exit(2)
def check_organization_language() -> None:
reader: Reader = injector.get(Reader)
with reader.get_database_context():
response = reader.get(GetRequest(ONE_ORGANIZATION_FQID, ["default_language"]))
if response["default_language"] != "en":
print("Cannot translate from source languages other than `en`.")
print_help()
sys.exit(3)
def print_help() -> None:
print("Usage: python translate.py <language>")
print(" Translates from 'en' to <language>.")
print(f""" language could be '{"', '".join(possible_languages)}'.""")
def main() -> None:
if len(sys.argv) != 2:
print_help()
sys.exit(1)
language = sys.argv[1]
check_language(language)
Translator.set_translation_language(language)
register_reader_services()
register_writer_services()
check_organization_language()
# translate and generate events
events = []
for collection in collection_to_fields_map:
fields = collection_to_fields_map[collection]
for id_, model in read_collection(collection, fields):
changed_fields: dict[str, Any] = {}
for field in fields:
old_value = model[field]
possible_new_value = Translator.translate(old_value)
if possible_new_value != old_value and possible_new_value is not None:
changed_fields[field] = possible_new_value
if collection == "organization":
changed_fields["default_language"] = language
elif collection == "meeting":
changed_fields["language"] = language
if changed_fields:
events.append(
RequestUpdateEvent(
fqid_from_collection_and_id(collection, id_),
changed_fields,
)
)
# write events into the datastore
if events:
write_request = WriteRequest(events, None, 0, {}) # type: ignore
writer: Writer = injector.get(Writer)
writer.write([write_request], log_all_modified_fields=False)
if __name__ == "__main__":
main()