| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import json
- import os
- import re
- def get_keys(data, prefix=""):
- keys = []
- if isinstance(data, dict):
- for k, v in data.items():
- new_key = f"{prefix}.{k}" if prefix else k
- # Skip language objects (they have "en", "ru", etc)
- if isinstance(v, dict) and any(lang in v for lang in ["en", "ru", "me", "ua"]):
- keys.append(new_key)
- else:
- keys.extend(get_keys(v, new_key))
- return keys
- def check_unused():
- locales_dir = "src/locales"
- src_dir = "src"
-
- trans_files = [f for f in os.listdir(locales_dir) if f.startswith("translations") and f.endswith(".json")]
-
- # Collect all source code content
- src_content = ""
- for root, dirs, files in os.walk(src_dir):
- for file in files:
- if file.endswith((".vue", ".ts", ".js")):
- with open(os.path.join(root, file), "r", encoding="utf-8") as f:
- src_content += f.read() + "\n"
- report = {}
-
- for trans_file in trans_files:
- path = os.path.join(locales_dir, trans_file)
- with open(path, "r", encoding="utf-8") as f:
- data = json.load(f)
-
- all_keys = get_keys(data)
- unused = []
-
- for key in all_keys:
- # Check for exact string match in code
- # We look for "key", 'key', or `key`
- pattern = re.compile(f"['\"`]{re.escape(key)}['\"`]")
- if not pattern.search(src_content):
- unused.append(key)
-
- report[trans_file] = unused
-
- return report
- if __name__ == "__main__":
- report = check_unused()
- print(json.dumps(report, indent=2, ensure_ascii=False))
|