pig.py/scripts/inject_translations.py

39 lines
1.4 KiB
Python

#!/usr/bin/env python3
"""Inject body translations into i18n.py"""
import re
from translations_body import BODY
def main():
with open('i18n.py', 'r', encoding='utf-8') as f:
content = f.read()
for lang, translations in BODY.items():
print(f"Processing {lang}...")
# Build the replacement text
lines = [' # About page body translations']
for key, value in translations.items():
escaped = value.replace('\\', '\\\\').replace('"', '\\"')
lines.append(f' "{key}": "{escaped}",')
new_block = '\n'.join(lines)
# Find the existing body translations for this language
# Pattern: from "# About page body translations" to "about_footer_remember": "...",
pattern = rf'( "{lang}": \{{[^}}]+?"about_footer_p5": "[^"]*",)\n # About page body translations\n.*?"about_footer_remember": "[^"]*",'
match = re.search(pattern, content, re.DOTALL)
if match:
old_block = match.group(0)
new_full = match.group(1) + '\n' + new_block
content = content.replace(old_block, new_full)
print(f" Updated {lang} with {len(translations)} translations")
else:
print(f" Could not find pattern for {lang}")
with open('i18n.py', 'w', encoding='utf-8') as f:
f.write(content)
print("Done!")
if __name__ == "__main__":
main()