1 Star 0 Fork 0

King/kflash_gui

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
translation.py 1.44 KB
一键复制 编辑 原始数据 按行查看 历史
neucrack 提交于 2019-07-15 14:13 . fix file close issue
import parameters, os, json
language_en = "en"
language_zh = "zh"
current_lang = language_en
lang_json_list = {}
for f in os.listdir(parameters.translationPathAbs):
lang = f.split(".")[0][12:]
if lang == "":
continue
f = parameters.translationPathAbs+"/"+f
with open(f,"r", encoding='utf-8') as fd:
try:
jsonObj = json.loads(fd.read())
except Exception as e:
print("tranlation:",f," format(json) error!")
raise e
lang_json_list[lang] = jsonObj
if len(lang_json_list) == 0:
raise Exception("No translation file!")
def setLanguage(language):
global current_lang
transFileName = "translation_"+language+".json"
for f in os.listdir(parameters.translationPathAbs):
if f.endswith(transFileName):
current_lang = language
def tr(str):
try:
return lang_json_list[current_lang][str]
except Exception:
try:
return lang_json_list[language_en][str]
except Exception:
return str
def tr_en(str):
return lang_json_list[language_en][str]
def tr2(str):
ret = str
key_find = None
max_find_len = 0
for key in lang_json_list[current_lang].keys():
if key in str:
if len(key) > max_find_len:
key_find = key
max_find_len = len(key)
if key_find:
ret = ret.replace(key_find, lang_json_list[current_lang][key_find])
return ret
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qiujianqin/kflash_gui.git
git@gitee.com:qiujianqin/kflash_gui.git
qiujianqin
kflash_gui
kflash_gui
master

搜索帮助