Files
dockerfluxodecaixapro/scripts/analyze_translations.py
T
2026-09-01 10:46:29 -03:00

102 lines
3.2 KiB
Python
Executable File

import os
import json
import glob
APP_PATH = r'c:\Dockers\dockersyscms\app'
MODULES_PATH = os.path.join(APP_PATH, 'modules')
def get_module_locales():
locales = {}
# Find all json files in modules/*/locales/*/*.json
pattern = os.path.join(MODULES_PATH, '*', 'locales', '*', '*.json')
files = glob.glob(pattern)
for file_path in files:
# Extract module, lang, filename
parts = file_path.split(os.sep)
# .../modules/{module}/locales/{lang}/{filename}.json
try:
locales_index = parts.index('locales')
module = parts[locales_index - 1]
lang = parts[locales_index + 1]
filename = parts[locales_index + 2]
key = f"{module}/{filename}"
if key not in locales:
locales[key] = {}
locales[key][lang] = file_path
except ValueError:
continue
return locales
def load_json(path):
try:
with open(path, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
print(f"Error reading {path}: {e}")
return {}
def flatten_keys(data, prefix=""):
keys = set()
if isinstance(data, dict):
for k, v in data.items():
full_key = f"{prefix}.{k}" if prefix else k
keys.add(full_key)
if isinstance(v, dict):
keys.update(flatten_keys(v, full_key))
return keys
def get_value_by_key(data, key):
parts = key.split('.')
curr = data
for part in parts:
if isinstance(curr, dict) and part in curr:
curr = curr[part]
else:
return None
return curr
def analyze_locales():
groups = get_module_locales()
report = {}
for group_key, langs in groups.items():
# Load all data
data_by_lang = {lang: load_json(path) for lang, path in langs.items()}
# Collect all unique keys
all_keys = set()
for data in data_by_lang.values():
all_keys.update(flatten_keys(data))
# Check for missing
group_report = {}
for lang, data in data_by_lang.items():
missing = []
for key in all_keys:
if get_value_by_key(data, key) is None:
# Find a fallback value from another language
fallback = None
for other_lang in ['en_US', 'pt_BR', 'es_ES', 'de_DE', 'fr_FR']:
if other_lang in data_by_lang:
val = get_value_by_key(data_by_lang[other_lang], key)
if val is not None:
fallback = val
break
missing.append({'key': key, 'fallback': fallback})
if missing:
group_report[lang] = missing
if group_report:
report[group_key] = group_report
return report
if __name__ == "__main__":
report = analyze_locales()
print(json.dumps(report, indent=2, ensure_ascii=False))