mirror of
https://github.com/sipeed/MaixCDK.git
synced 2026-11-09 17:04:38 -06:00
update i18n support load from yaml files, add trans search tool maixtool i18n command
This commit is contained in:
49
tools/cmake/copy_assets.py
Normal file
49
tools/cmake/copy_assets.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
import yaml
|
||||
|
||||
def copy_assets(project_path, dist_path):
|
||||
app_yaml_path = os.path.join(project_path, 'app.yaml')
|
||||
|
||||
# 检查 app.yaml 是否存在
|
||||
if not os.path.exists(app_yaml_path):
|
||||
print(f"{app_yaml_path} not a app, skip copy files")
|
||||
return
|
||||
|
||||
# 读取 app.yaml 文件
|
||||
with open(app_yaml_path, 'r') as file:
|
||||
config = yaml.safe_load(file)
|
||||
|
||||
# 检查 assets 键是否存在
|
||||
if 'files' not in config:
|
||||
print("No 'files' key found in app.yaml, skip copy files")
|
||||
return
|
||||
|
||||
assets = config.get('files', [])
|
||||
|
||||
if isinstance(assets, list):
|
||||
for item in assets:
|
||||
src = os.path.join(project_path, item)
|
||||
dest = os.path.join(dist_path, item)
|
||||
if os.path.exists(src):
|
||||
shutil.copytree(src, dest, dirs_exist_ok=True)
|
||||
print(f"Copied {src} to {dest}")
|
||||
else:
|
||||
raise Exception(f"{src} does not exist.")
|
||||
elif isinstance(assets, dict):
|
||||
for src_key, dest_value in assets.items():
|
||||
src = os.path.join(project_path, src_key)
|
||||
dest = os.path.join(dist_path, dest_value)
|
||||
if os.path.exists(src):
|
||||
shutil.copytree(src, dest, dirs_exist_ok=True)
|
||||
print(f"Copied {src} to {dest}")
|
||||
else:
|
||||
raise Exception(f"{src} does not exist.")
|
||||
else:
|
||||
print("'files' key must be a list or a dictionary.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
project_path = sys.argv[1]
|
||||
dist_path = sys.argv[2]
|
||||
copy_assets(project_path, dist_path)
|
||||
@@ -25,12 +25,15 @@ if(${BUILD_TYPE} STREQUAL "Release")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(cp_assets_cmd COMMAND python ${SDK_PATH}/tools/cmake/copy_assets.py ${PROJECT_PATH} ${PROJECT_DIST_DIR}/${PROJECT_ID}_${build_type})
|
||||
|
||||
add_custom_command(TARGET ${PROJECT_ID} POST_BUILD
|
||||
${strip_cmd}
|
||||
COMMAND mkdir -p ${CMAKE_BINARY_DIR}/dl_lib
|
||||
${cp_command}
|
||||
${cp_dist_cmd}
|
||||
${cp_dl_to_dist_cmd}
|
||||
${cp_assets_cmd}
|
||||
DEPENDS ${PROJECT_ID}
|
||||
COMMENT "-- copy dynamic libs to build/dl_lib dir ...")
|
||||
|
||||
|
||||
101
tools/maixtool/maixtool/i18n.py
Normal file
101
tools/maixtool/maixtool/i18n.py
Normal 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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
|
||||
|
||||
__version__ = "1.3.3"
|
||||
__version__ = "1.3.4"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user