Host snippets with a git backend. Like Gists but OpenSource. https://gist.sigpipe.me/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i18n.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python
  2. import re
  3. import configparser
  4. import pprint
  5. import os
  6. # START OF CONFIG
  7. """
  8. Examples for each regexp match:
  9. 1.
  10. 2.
  11. """
  12. RES = {
  13. ".tmpl": re.compile(r"{{\s*\.i18n\.Tr\s+\"(\w*)\.([a-zA-Z_-]*)\"\s*}}"),
  14. ".go": re.compile(r"ctx\.Tr\(\"(\w*)\.(.*)\"")
  15. }
  16. INI_PARSE = [
  17. "conf/locale/locale_en-US.ini",
  18. "conf/locale/locale_fr-FR.ini",
  19. ]
  20. DIRS_SEARCH = [
  21. "cmd",
  22. "models",
  23. "routers",
  24. "setting",
  25. "stuff",
  26. "templates",
  27. ]
  28. EXT_ALLOWED = [".go", ".tmpl"]
  29. # END OF CONFIG
  30. # Global variables
  31. """
  32. [{filename, obj},]
  33. """
  34. ini_parsed = []
  35. """
  36. [{ini, filename, section, option, kind},]
  37. """
  38. unknown = []
  39. # 1. Parse ini file
  40. print("== Parsing {num} INI local files".format(num=len(INI_PARSE)))
  41. for i in INI_PARSE:
  42. a = configparser.ConfigParser()
  43. a.read(i)
  44. ini_parsed.append({'filename': i, 'obj': a})
  45. print("= {filename}".format(filename=i))
  46. print("")
  47. def compare_inis(match, fname):
  48. for i in ini_parsed:
  49. try:
  50. kv = i['obj'].get(match[0], match[1])
  51. except configparser.NoOptionError:
  52. unknown.append({'ini': i['filename'], 'filename': fname, 'section': match[0], 'option': match[1], 'kind': 'option'})
  53. except configparser.NoSectionError:
  54. unknown.append({'ini': i['filename'], 'filename': fname, 'section': match[0], 'option': match[1], 'kind': 'section'})
  55. def parse_file(path, fname, ext):
  56. filename = os.path.join(path, fname)
  57. print("= Parsing {filename}".format(filename=filename))
  58. with open(filename, 'r') as f:
  59. for line in f:
  60. match = RES[ext].findall(line)
  61. if match and len(match) >= 1:
  62. for m in match:
  63. compare_inis(m, filename)
  64. # 2. Parse each dirs - walking sub
  65. for directory in DIRS_SEARCH:
  66. for root, dirs, files in os.walk(directory):
  67. for fname in files:
  68. ext = os.path.splitext(fname)[1]
  69. if ext in EXT_ALLOWED:
  70. parse_file(root, fname, ext)
  71. # 3. Show non-matched
  72. print("")
  73. if len(unknown) <= 0:
  74. print("= No unmatched i18n strings detected")
  75. else:
  76. print("= Unmatched strings:")
  77. for u in unknown:
  78. print("From file '{filename}' with ini locale '{ini}', missing [{section}] {option}".format(filename=u['filename'], ini=u['ini'], section=u['section'], option=u['option']))