-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslations.py
66 lines (56 loc) · 2.06 KB
/
translations.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
"""
This script translates messages from a .pot file to Spanish using the
GoogleTranslator from the deep_translator library.
Steps:
1. Read the content of a .pot file which contains msgid entries.
2. Use regular expressions to extract all msgid entries.
3. Translate each msgid entry from English to Spanish.
4. Write the translations into a new .po file with the appropriate formatting.
Dependencies:
- deep_translator: A Python library that provides a simple interface to various
translation services, including Google Translate.
- re: The built-in Python module for regular expressions, used to find msgid
entries in the .pot file.
"""
import re
from deep_translator import GoogleTranslator
with open("messages.pot", "r", encoding="utf-8") as file:
pot_content = file.read()
# Find all msgid entries using regex
msgid_pattern = re.compile(r'msgid "(.*?)"')
msgids = msgid_pattern.findall(pot_content)
# Translate each msgid entry to Spanish
translations = {}
for msgid in msgids:
translation = GoogleTranslator(source="en", target="es").translate(msgid)
translations[msgid] = translation
# Write the translated content to a new .po file
with open(
"app/translations/es/LC_MESSAGES/messages.po", "w", encoding="utf-8"
) as po_file:
po_file.write(
"""# Translations for PROJECT
# Generated by script
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\\n"
"POT-Creation-Date: 2024-12-19 16:52+0200\\n"
"PO-Revision-Date: 2024-12-19 19:35+0200\\n"
"Last-Translator: YOUR_NAME <YOUR_EMAIL>\\n"
"Language-Team: Spanish <[email protected]>\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Generated-By: Babel 2.16.0\\n"
"Language: es\\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\\n"
"""
)
# Add translated msgid and msgstr pairs
for msgid in msgids:
msgstr = translations.get(msgid, "")
po_file.write("\n#: Generated\n")
po_file.write(f'msgid "{msgid}"\n')
po_file.write(f'msgstr "{msgstr}"\n')
print("Translation complete!")