- rules_benedict.py: Source translations for all 72 rules in 27 languages - inject_rules.py: Script to inject rules into i18n.py TRANSLATIONS - i18n.py: Added rule_1..rule_72 for all language dicts - about.html.j2: Fixed HTML escaping with |safe filter for rules
70 lines
2 KiB
Python
70 lines
2 KiB
Python
#!/usr/bin/env python3
|
|
"""Inject Benedict rules from rules_benedict.py into i18n.py."""
|
|
|
|
import re
|
|
|
|
def inject_rules():
|
|
# Import rules
|
|
from rules_benedict import RULES
|
|
|
|
# Read current i18n.py
|
|
with open("i18n.py", "r") as f:
|
|
content = f.read()
|
|
|
|
# For each language in RULES, add the rules to the corresponding language in TRANSLATIONS
|
|
for lang, rules in RULES.items():
|
|
if lang == "en":
|
|
continue # Already added
|
|
|
|
print(f"Processing {lang}...")
|
|
|
|
# Find the language dict in i18n.py
|
|
lang_pattern = f'"{lang}": {{'
|
|
lang_start = content.find(lang_pattern)
|
|
if lang_start == -1:
|
|
print(f" Could not find '{lang}' in i18n.py")
|
|
continue
|
|
|
|
# Find the closing of this language dict
|
|
brace_count = 0
|
|
pos = lang_start
|
|
in_lang = False
|
|
insert_pos = None
|
|
for i, ch in enumerate(content[lang_start:]):
|
|
if ch == '{':
|
|
brace_count += 1
|
|
in_lang = True
|
|
elif ch == '}':
|
|
brace_count -= 1
|
|
if brace_count == 0 and in_lang:
|
|
insert_pos = lang_start + i
|
|
break
|
|
|
|
if insert_pos is None:
|
|
print(f" Could not find end of '{lang}' dict")
|
|
continue
|
|
|
|
# Check if rules already exist
|
|
if f'"rule_1":' in content[lang_start:insert_pos]:
|
|
print(f" Rules already exist for {lang}, skipping")
|
|
continue
|
|
|
|
# Generate rules string
|
|
rules_str = ""
|
|
for key, value in rules.items():
|
|
# Escape quotes in value
|
|
escaped = value.replace('\\', '\\\\').replace('"', '\\"')
|
|
rules_str += f' "{key}": "{escaped}",\n'
|
|
|
|
# Insert before the closing }
|
|
content = content[:insert_pos] + rules_str + content[insert_pos:]
|
|
print(f" Added {len(rules)} rules")
|
|
|
|
# Write updated content
|
|
with open("i18n.py", "w") as f:
|
|
f.write(content)
|
|
|
|
print("Done!")
|
|
|
|
if __name__ == "__main__":
|
|
inject_rules()
|