scan_i18n.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import ast
  2. import glob
  3. import json
  4. from collections import OrderedDict
  5. def extract_i18n_strings(node):
  6. i18n_strings = []
  7. if (
  8. isinstance(node, ast.Call)
  9. and isinstance(node.func, ast.Name)
  10. and node.func.id == "i18n"
  11. ):
  12. for arg in node.args:
  13. if isinstance(arg, ast.Str):
  14. i18n_strings.append(arg.s)
  15. for child_node in ast.iter_child_nodes(node):
  16. i18n_strings.extend(extract_i18n_strings(child_node))
  17. return i18n_strings
  18. # scan the directory for all .py files (recursively)
  19. # for each file, parse the code into an AST
  20. # for each AST, extract the i18n strings
  21. strings = []
  22. for filename in glob.iglob("**/*.py", recursive=True):
  23. with open(filename, "r") as f:
  24. code = f.read()
  25. if "I18nAuto" in code:
  26. tree = ast.parse(code)
  27. i18n_strings = extract_i18n_strings(tree)
  28. print(filename, len(i18n_strings))
  29. strings.extend(i18n_strings)
  30. code_keys = set(strings)
  31. """
  32. n_i18n.py
  33. gui_v1.py 26
  34. app.py 16
  35. infer-web.py 147
  36. scan_i18n.py 0
  37. i18n.py 0
  38. lib/train/process_ckpt.py 1
  39. """
  40. print()
  41. print("Total unique:", len(code_keys))
  42. standard_file = "i18n/locale/zh_CN.json"
  43. with open(standard_file, "r", encoding="utf-8") as f:
  44. standard_data = json.load(f, object_pairs_hook=OrderedDict)
  45. standard_keys = set(standard_data.keys())
  46. # Define the standard file name
  47. unused_keys = standard_keys - code_keys
  48. print("Unused keys:", len(unused_keys))
  49. for unused_key in unused_keys:
  50. print("\t", unused_key)
  51. missing_keys = code_keys - standard_keys
  52. print("Missing keys:", len(missing_keys))
  53. for missing_key in missing_keys:
  54. print("\t", missing_key)
  55. code_keys_dict = OrderedDict()
  56. for s in strings:
  57. code_keys_dict[s] = s
  58. # write back
  59. with open(standard_file, "w", encoding="utf-8") as f:
  60. json.dump(code_keys_dict, f, ensure_ascii=False, indent=4, sort_keys=True)
  61. f.write("\n")