locale_diff.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import json
  2. import os
  3. from collections import OrderedDict
  4. # Define the standard file name
  5. standard_file = "locale/zh_CN.json"
  6. # Find all JSON files in the directory
  7. dir_path = "locale/"
  8. languages = [
  9. os.path.join(dir_path, f)
  10. for f in os.listdir(dir_path)
  11. if f.endswith(".json") and f != standard_file
  12. ]
  13. # Load the standard file
  14. with open(standard_file, "r", encoding="utf-8") as f:
  15. standard_data = json.load(f, object_pairs_hook=OrderedDict)
  16. # Loop through each language file
  17. for lang_file in languages:
  18. # Load the language file
  19. with open(lang_file, "r", encoding="utf-8") as f:
  20. lang_data = json.load(f, object_pairs_hook=OrderedDict)
  21. # Find the difference between the language file and the standard file
  22. diff = set(standard_data.keys()) - set(lang_data.keys())
  23. miss = set(lang_data.keys()) - set(standard_data.keys())
  24. # Add any missing keys to the language file
  25. for key in diff:
  26. lang_data[key] = key
  27. # Del any extra keys to the language file
  28. for key in miss:
  29. del lang_data[key]
  30. # Sort the keys of the language file to match the order of the standard file
  31. lang_data = OrderedDict(
  32. sorted(lang_data.items(), key=lambda x: list(standard_data.keys()).index(x[0]))
  33. )
  34. # Save the updated language file
  35. with open(lang_file, "w", encoding="utf-8") as f:
  36. json.dump(lang_data, f, ensure_ascii=False, indent=4, sort_keys=True)
  37. f.write("\n")