Browse Source

Add the script I use for debugging i18n

Dashie der otter 1 year ago
parent
commit
5c19530adb
Signed by: Dashie <dashie@sigpipe.me> GPG Key ID: C2D57B325840B755
1 changed files with 88 additions and 0 deletions
  1. 88
    0
      i18n.py

+ 88
- 0
i18n.py View File

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

Loading…
Cancel
Save