Files
intaleq_driver/scratch/fix_translations_syntax.py

83 lines
2.7 KiB
Python

import re
filepath = 'lib/controller/local/translations.dart'
with open(filepath, 'r', encoding='utf-8') as f:
lines = f.readlines()
new_lines = []
seen_keys_en = set()
seen_keys_ar = set()
in_en = False
in_ar = False
for line in lines:
if '"en": {' in line:
in_en = True
in_ar = False
new_lines.append(line)
continue
if '"ar": {' in line:
in_en = False
in_ar = True
new_lines.append(line)
continue
if '};' in line or ' }' in line:
if in_en or in_ar:
if ' }' in line:
in_en = False
in_ar = False
new_lines.append(line)
continue
# Try to extract key and value using a more careful approach
# Looking for: "key": "value",
# We find the first " and the last " and the middle separator ": "
stripped = line.strip()
if stripped.startswith('"') and (stripped.endswith('",') or stripped.endswith('"')):
# Split by ": " but only once
try:
# We need to find the ": " that separates key and value
# Since keys and values can contain ": ", we look for the one surrounded by quotes
# A common pattern is "...": "..."
parts = re.split(r'":\s+"', stripped)
if len(parts) >= 2:
# Key is from index 1 to second-to-last char of first part
key = parts[0][1:]
# Value is from index 0 to last char (excluding optional comma and final quote)
val_part = parts[1]
if val_part.endswith(','):
val = val_part[:-2]
comma = ','
else:
val = val_part[:-1]
comma = ''
# FIX 1: Trailing backslashes
if key.endswith('\\') and not key.endswith('\\\\'):
key += '\\'
if val.endswith('\\') and not val.endswith('\\\\'):
val += '\\'
# FIX 2: Unescaped dollar signs
key = re.sub(r'(?<!\\)\$', r'\$', key)
val = re.sub(r'(?<!\\)\$', r'\$', val)
# FIX 3: Deduplication
if in_en:
if key in seen_keys_en: continue
seen_keys_en.add(key)
if in_ar:
if key in seen_keys_ar: continue
seen_keys_ar.add(key)
new_lines.append(f' "{key}": "{val}"{comma}\n')
continue
except:
pass
new_lines.append(line)
with open(filepath, 'w', encoding='utf-8') as f:
f.writelines(new_lines)