rewrite: Localisation scripts.

This commit is contained in:
2023-05-25 16:46:12 +03:00
parent fe8cf35885
commit 3142140849
4 changed files with 168 additions and 7 deletions

76
scripts/makedomains.py Normal file
View File

@@ -0,0 +1,76 @@
# Iterate through each file in the project
# Look for LocalBabel("text") in each file, mark it as the "text" domain
# module files inherit from their __init__ files if they don't specify a specific domain
import re
import os
from collections import defaultdict
babel_regex = re.compile(
r".*LocalBabel\(\s*['\"](.*)['\"]\s*\).*"
)
ignored_dirs = {
'RCS',
'__pycache__',
'pending-rewrite',
'.git'
}
def parse_domain(path):
"""
Parse a file to check for domain specifications.
Currently just looks for a LocalBabel("domain") specification.
"""
with open(path, 'r') as file:
for line in file:
match = babel_regex.match(line)
if match:
return match.groups()[0]
def read_directory(path, domain_map, domain='base'):
init_path = None
files = []
dirs = []
for entry in os.scandir(path):
if entry.is_file(follow_symlinks=False) and entry.name.endswith('.py'):
if entry.name == '__init__.py':
init_path = entry.path
else:
files.append(entry.path)
elif entry.is_dir(follow_symlinks=False) and entry.name not in ignored_dirs:
dirs.append(entry.path)
if init_path:
domain = parse_domain(init_path) or domain
print(
f"{domain:<20} | {path}"
)
for file_path in files:
file_domain = parse_domain(file_path) or domain
print(
f"{file_domain:<20} | {file_path}"
)
domain_map[file_domain].append(file_path)
for dir_path in dirs:
read_directory(dir_path, domain_map, domain)
def write_domains(domain_map):
for domain, files in domain_map.items():
domain_path = os.path.join('locales', 'domains', f"{domain}.txt")
with open(domain_path, 'w') as domain_file:
domain_file.write('\n'.join(files))
print(f"Wrote {len(files)} source files to {domain_path}")
if __name__ == '__main__':
domain_map = defaultdict(list)
read_directory('src/', domain_map, domain='base')
write_domains(domain_map)
print("Supported domains: {}".format(', '.join(domain_map.keys())))

View File

@@ -1,7 +1,7 @@
xgettext src/modules/sysadmin/exec_cog.py -o locales/templates/exec.pot --keyword=_p:1c,2 --keyword=_n:1,2 --keyword=_np:1c,2,3 #!/bin/bash
xgettext src/babel/*.py -o locales/templates/babel.pot --keyword=_p:1c,2 --keyword=_n:1,2 --keyword=_np:1c,2,3
xgettext src/modules/reminders/*.py -o locales/templates/reminders.pot --keyword=_p:1c,2 --keyword=_n:1,2 --keyword=_np:1c,2,3 for filename in locales/domains/*.txt; do
xgettext src/gui/cards/stats.py -o locales/templates/stats-gui.pot --keyword=_p:1c,2 --keyword=_n:1,2 --keyword=_np:1c,2,3 domain=$(basename "$filename" .txt)
xgettext src/gui/cards/weekly.py -o locales/templates/weekly-gui.pot --keyword=_p:1c,2 --keyword=_n:1,2 --keyword=_np:1c,2,3 echo "Creating template for domain: $domain"
xgettext src/gui/cards/monthly.py -o locales/templates/monthly-gui.pot --keyword=_p:1c,2 --keyword=_n:1,2 --keyword=_np:1c,2,3 xgettext -f $filename -o locales/templates/$domain.pot --keyword=_p:1c,2 --keyword=_n:1,2 --keyword=_np:1c,2,3 -v -c --debug
xgettext src/gui/cards/goals.py -o locales/templates/goals-gui.pot --keyword=_p:1c,2 --keyword=_n:1,2 --keyword=_np:1c,2,3 done

70
scripts/maketestlang.py Normal file
View File

@@ -0,0 +1,70 @@
import os
import string
import polib
templates = os.path.join('locales', 'templates')
test_target = os.path.join('locales', 'ceaser', 'LC_MESSAGES')
def translate_string(msgid: str) -> str:
tokens = []
lifted = False
for c in msgid:
if c in ('{', '}'):
lifted = not lifted
elif not lifted and c.isalpha():
if c.isupper():
letters = string.ascii_uppercase
else:
letters = string.ascii_lowercase
index = letters.index(c)
c = letters[(index + 1) % len(letters)]
tokens.append(c)
translated = ''.join(tokens)
return translated
def translate_entry(entry: polib.POEntry):
if entry.msgctxt and ('regex' in entry.msgctxt):
# Ignore
...
else:
if entry.msgid:
entry.msgstr = translate_string(entry.msgid)
def process_pot(domain, path):
print(f"Processing pot for {domain}")
entries = 0
po = polib.pofile(path, encoding="UTF-8")
po.metadata = {
'Project-Id-Version': '1.0',
'Report-Msgid-Bugs-To': 'you@example.com',
'POT-Creation-Date': '2007-10-18 14:00+0100',
'PO-Revision-Date': '2007-10-18 14:00+0100',
'Last-Translator': 'you <you@example.com>',
'Language-Team': 'English <yourteam@example.com>',
'MIME-Version': '1.0',
'Content-Type': 'text/plain; charset=utf-8',
'Content-Transfer-Encoding': '8bit',
}
for entry in po.untranslated_entries():
entries += 1
translate_entry(entry)
# Now save
targetpo = os.path.join(test_target, f"{domain}.po")
targetmo = os.path.join(test_target, f"{domain}.mo")
po.save(targetpo)
po.save_as_mofile(targetmo)
print(f"Processed {entries} from POT {domain}.")
def process_all():
for file in os.scandir(templates):
if file.name.endswith('pot'):
print(f"Processing pot: {file.name[:-4]}")
process_pot(file.name[:-4], file.path)
if __name__ == '__main__':
process_all()

15
scripts/start_leo_debug.py Executable file
View File

@@ -0,0 +1,15 @@
# !/bin/python3
import sys
import os
import tracemalloc
tracemalloc.start()
sys.path.insert(0, os.path.join(os.getcwd()))
sys.path.insert(0, os.path.join(os.getcwd(), "src"))
if __name__ == '__main__':
from bot import _main
_main()