update i18n support load from yaml files, add trans search tool maixtool i18n command

This commit is contained in:
Neucrack
2024-07-12 22:20:25 +08:00
parent efb2135d03
commit 1b64a6cd0f
15 changed files with 463 additions and 104 deletions

View File

@@ -0,0 +1,101 @@
import sys, os
import yaml
from collections import OrderedDict
def get_files(scan_dir, recursive, exts=[".c", ".cpp", ".h", ".hpp"]):
files = []
if recursive:
for root, dirs, fs in os.walk(scan_dir):
for f in fs:
if os.path.splitext(f)[-1] in exts:
files.append(os.path.join(root, f))
else:
for name in os.listdir(scan_dir):
if os.path.splitext(name)[1] in exts:
files.append(os.path.join(scan_dir, name))
return files
def get_i18n_strs(file, keywords):
'''
read content and find '_("xxx")', get all xxx and return
'''
strs = []
with open(file, "r") as f:
content = f.read()
for key in keywords:
idx = 0
while True:
flag = False
idx = content.find(f'{key}("', idx)
if idx == -1:
idx = content.find(f"{key}('", idx)
if idx == -1:
break
flag = True
if not (idx == 0 or content[idx - 1] in [".", ",", " ", ">"]): # maybe like str etc.
idx += 1
continue
idx += len(key) + 2
if flag:
end = content.find("')", idx)
else:
end = content.find('")', idx)
if end == -1:
break
strs.append(content[idx:end])
idx = end
return strs
def main(search_dir, keywords, exts, out, recursive, locales):
print("seach dir: ", dir)
print("keywords: ", keywords)
print("extentions:", exts)
print("out dir: ", out)
print("")
files = get_files(search_dir, recursive)
print(f"{len(files)} files found")
keys_list = []
for f in files:
strs = get_i18n_strs(f, keywords)
keys_list.extend(strs)
keys_list = list(set(keys_list))
keys_list.sort()
keys_dict = {}
for i in keys_list:
keys_dict[i] = i
os.makedirs(out, exist_ok=True)
for locale in locales:
print(f"gen locale [ {locale} ]")
path = os.path.join(out, f"{locale}.yaml")
old = {}
if os.path.exists(path):
with open(path, "r", encoding="utf-8") as f:
old = yaml.safe_load(f)
if old is None:
old = {}
# update by old
for k in keys_dict:
if k in old:
keys_dict[k] = old[k]
with open(path, "w", encoding="utf-8") as f:
yaml.dump(keys_dict, f, allow_unicode=True)
if __name__ == "__main__":
import argparse
paser = argparse.ArgumentParser()
paser.add_argument("-k", "--keywords", nargs="+", type=str, default=["_", "tr"], help="translate function keywords to search")
paser.add_argument("-r", action="store_true", help="recursive search dir")
paser.add_argument("-e", "--exts", nargs="+", type=str, default=[".c", ".cpp", ".h", ".hpp", ".py"], help="file extention to search")
paser.add_argument("-l", "--locales", nargs="+", type=str, default=["en", "zh"], help="locals of region, like en zh ja etc.")
paser.add_argument("-o", "--out", type=str, default="locales", help="translation files output directory")
paser.add_argument("-d", "--dir", type=str, help="where to search", required=True)
args = paser.parse_args()
main(args.dir, args.keywords, args.exts, args.out, args.r, args.locales)

View File

@@ -3,6 +3,7 @@ from .version import __version__
import os
from .app_deploy import serve
from .app_release import pack
from .i18n import main as i18n_main
def deploy(port, pkg_path):
if not pkg_path:
@@ -15,15 +16,29 @@ def deploy(port, pkg_path):
serve(pkg_path, port)
def main():
cmds = ["release", "deploy"]
parser = argparse.ArgumentParser(description='maixtool, tools for maix series development')
parser.add_argument('-v', '--version', action='version', version=__version__)
parser.add_argument('-p', "--port", default=8888, type=int, help="deploy server port")
parser.add_argument("--pkg", default="", type=str, help="app package path")
parser.add_argument("command", help="command to run", choices=cmds)
args = parser.parse_args()
if args.command == "release":
pack(os.getcwd())
elif args.command == "deploy":
deploy(args.port, args.pkg)
subparsers = parser.add_subparsers(help='command help', dest="cmd")
# release
parser_release = subparsers.add_parser("release", help="release APP")
# deploy
parser_deploy = subparsers.add_parser("deploy", help="release APP")
parser_deploy.add_argument('-p', "--port", default=8888, type=int, help="deploy server port")
parser_deploy.add_argument("--pkg", default="", type=str, help="app package path")
# i18n
parser_i18n = subparsers.add_parser("i18n", help="release APP")
parser_i18n.add_argument("-k", "--keywords", nargs="+", type=str, default=["_", "tr"], help="translate function keywords to search")
parser_i18n.add_argument("-r", action="store_true", help="recursive search dir")
parser_i18n.add_argument("-e", "--exts", nargs="+", type=str, default=[".c", ".cpp", ".h", ".hpp", ".py"], help="file extention to search")
parser_i18n.add_argument("-l", "--locales", nargs="+", type=str, default=["en", "zh"], help="locals of region, like en zh ja etc.")
parser_i18n.add_argument("-o", "--out", type=str, default="locales", help="translation files output directory")
parser_i18n.add_argument("-d", "--dir", type=str, help="where to search", required=True)
args = parser.parse_args()
if args.cmd == "release":
pack(os.getcwd())
elif args.cmd == "deploy":
deploy(args.port, args.pkg)
elif args.cmd == "i18n":
i18n_main(args.dir, args.keywords, args.exts, args.out, args.r, args.locales)

View File

@@ -1,4 +1,4 @@
__version__ = "1.3.3"
__version__ = "1.3.4"