61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
import re
|
|
import json
|
|
|
|
def extract_language_maps(filepath):
|
|
with open(filepath, 'r', encoding='utf-8') as f:
|
|
content = f.read()
|
|
|
|
# Let's find language blocks like: "en": { ... }, "fr": { ... }
|
|
# To parse these blocks, we find the starting position of each language identifier
|
|
# and scan until the matching closing brace.
|
|
languages = ['tr', 'fr', 'de', 'es', 'fa', 'el', 'ur', 'hi', 'ru', 'it', 'zh']
|
|
extracted = {}
|
|
|
|
for lang in languages:
|
|
# Regex to find: "lang": { or 'lang': {
|
|
pattern = re.compile(r'[\'"]' + lang + r'[\'"]\s*:\s*\{')
|
|
match = pattern.search(content)
|
|
if not match:
|
|
print(f"Language {lang} map start not found!")
|
|
continue
|
|
|
|
start_idx = match.end()
|
|
# Find matching closing brace
|
|
brace_count = 1
|
|
current_idx = start_idx
|
|
while brace_count > 0 and current_idx < len(content):
|
|
char = content[current_idx]
|
|
if char == '{':
|
|
brace_count += 1
|
|
elif char == '}':
|
|
brace_count -= 1
|
|
current_idx += 1
|
|
|
|
block = content[start_idx:current_idx-1]
|
|
|
|
# Now parse the key-value pairs inside the block
|
|
# Example: "key": "value", or 'key': 'value',
|
|
# Handle escaped quotes.
|
|
# Pattern: (['"])(.*?)\1\s*:\s*(['"])(.*?)\3\s*(?:,|$)
|
|
kv_pattern = re.compile(r'^\s*([\'"])(.*?)\1\s*:\s*([\'"])(.*?)\3\s*,?\s*$', re.MULTILINE)
|
|
kv_matches = kv_pattern.findall(block)
|
|
|
|
lang_map = {}
|
|
for kv in kv_matches:
|
|
key = kv[1]
|
|
val = kv[3]
|
|
# Unescape quotes
|
|
key = key.replace('\\"', '"').replace("\\'", "'")
|
|
val = val.replace('\\"', '"').replace("\\'", "'")
|
|
lang_map[key] = val
|
|
|
|
extracted[lang] = lang_map
|
|
print(f"Extracted {lang}: {len(lang_map)} keys")
|
|
|
|
return extracted
|
|
|
|
extracted = extract_language_maps("scratch/legacy_translations.dart")
|
|
with open("scratch/legacy_extracted_languages.json", "w", encoding="utf-8") as f:
|
|
json.dump(extracted, f, ensure_ascii=False, indent=2)
|
|
print("Saved legacy extracted languages to JSON.")
|