Browse Source

Add the script I use for debugging i18n

tags/v0.4.3
Dashie der otter 2 years 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 @@
#!/usr/bin/env python
import re
import configparser
import pprint
import os

# START OF CONFIG
"""
Examples for each regexp match:
1.

2.
"""
RES = {
".tmpl": re.compile(r"{{\s*\.i18n\.Tr\s+\"(\w*)\.([a-zA-Z_-]*)\"\s*}}"),
".go": re.compile(r"ctx\.Tr\(\"(\w*)\.(.*)\"")
}
INI_PARSE = [
"conf/locale/locale_en-US.ini",
"conf/locale/locale_fr-FR.ini",
]
DIRS_SEARCH = [
"cmd",
"models",
"routers",
"setting",
"stuff",
"templates",
]
EXT_ALLOWED = [".go", ".tmpl"]
# END OF CONFIG

# Global variables
"""
[{filename, obj},]
"""
ini_parsed = []

"""
[{ini, filename, section, option, kind},]
"""
unknown = []

# 1. Parse ini file
print("== Parsing {num} INI local files".format(num=len(INI_PARSE)))
for i in INI_PARSE:
a = configparser.ConfigParser()
a.read(i)
ini_parsed.append({'filename': i, 'obj': a})
print("= {filename}".format(filename=i))

print("")

def compare_inis(match, fname):
for i in ini_parsed:
try:
kv = i['obj'].get(match[0], match[1])
except configparser.NoOptionError:
unknown.append({'ini': i['filename'], 'filename': fname, 'section': match[0], 'option': match[1], 'kind': 'option'})
except configparser.NoSectionError:
unknown.append({'ini': i['filename'], 'filename': fname, 'section': match[0], 'option': match[1], 'kind': 'section'})

def parse_file(path, fname, ext):
filename = os.path.join(path, fname)
print("= Parsing {filename}".format(filename=filename))
with open(filename, 'r') as f:
for line in f:
match = RES[ext].findall(line)
if match and len(match) >= 1:
for m in match:
compare_inis(m, filename)

# 2. Parse each dirs - walking sub
for directory in DIRS_SEARCH:
for root, dirs, files in os.walk(directory):
for fname in files:
ext = os.path.splitext(fname)[1]
if ext in EXT_ALLOWED:
parse_file(root, fname, ext)

# 3. Show non-matched
print("")
if len(unknown) <= 0:
print("= No unmatched i18n strings detected")
else:
print("= Unmatched strings:")
for u in unknown:
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