Files
intaleq/scripts/fix_translation_keys.py

83 lines
2.7 KiB
Python

import os
import re
import subprocess
TRANS_DIRS = [
"/Users/hamzaaleghwairyeen/development/App/IntaleqApp/intaleq_driver/lib/controller/local",
"/Users/hamzaaleghwairyeen/development/App/IntaleqApp/intaleq_rider/lib/controller/local",
]
def get_git_head_content(rel_path):
try:
res = subprocess.run(
["git", "show", f"HEAD:{rel_path}"],
capture_output=True,
text=True,
check=True
)
return res.stdout
except Exception as e:
print(f"Error getting git head for {rel_path}: {e}")
return None
def process_translation_file(filepath):
repo_root = "/Users/hamzaaleghwairyeen/development/App/IntaleqApp"
rel_path = os.path.relpath(filepath, repo_root)
head_content = get_git_head_content(rel_path)
if not head_content:
return
with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
current_lines = f.readlines()
head_lines = head_content.splitlines(keepends=True)
if len(current_lines) != len(head_lines):
print(f"Line count mismatch for {rel_path} ({len(current_lines)} vs {len(head_lines)})")
return
new_lines = []
restored_keys = 0
# Pattern for map entries: "key": "value",
pattern = re.compile(r'^\s*"(.*?)":\s*"(.*?)",?\s*$')
for curr_line, head_line in zip(current_lines, head_lines):
m_curr = pattern.match(curr_line)
m_head = pattern.match(head_line)
if m_curr and m_head:
orig_key, orig_val = m_head.group(1), m_head.group(2)
curr_key, curr_val = m_curr.group(1), m_curr.group(2)
if orig_key != curr_key:
# Key was changed! Restore orig_key, but keep curr_val
# Preserve leading indent & formatting
indent = curr_line[:curr_line.find('"')]
has_comma = curr_line.rstrip().endswith(",")
comma = "," if has_comma else ""
fixed_line = f'{indent}"{orig_key}": "{curr_val}"{comma}\n'
new_lines.append(fixed_line)
restored_keys += 1
else:
new_lines.append(curr_line)
else:
new_lines.append(curr_line)
if restored_keys > 0:
with open(filepath, "w", encoding="utf-8") as f:
f.writelines(new_lines)
print(f"Fixed {restored_keys} keys in {rel_path}")
def main():
print("Fixing translation keys in driver & rider local translation files...\n")
for d in TRANS_DIRS:
for file in os.listdir(d):
if file.endswith(".dart"):
filepath = os.path.join(d, file)
process_translation_file(filepath)
if __name__ == "__main__":
main()