新语言:机翻

语言模块升级
支持更自由地添加语言(不再使用语言序号)
给getTip设置metatable的代码移出模块
This commit is contained in:
MrZ626
2021-09-20 20:25:17 +08:00
parent 493fcb2f05
commit 7393b3c716
14 changed files with 1053 additions and 107 deletions

View File

@@ -1,5 +1,7 @@
local LANG={}
function LANG.init(langList,publicText)--Attention, calling this will destory all initializing methods, create a LANG.set()!
--ONLY FIRST CALL MAKE SENSE
--Create LANG.get() and LANG.addScene()
function LANG.init(defaultLang,langList,publicText,pretreatFunc)
local function _langFallback(T0,T)
for k,v in next,T0 do
if type(v)=='table'and not v.refuseCopy then--refuseCopy: just copy pointer, not contents
@@ -12,47 +14,36 @@ function LANG.init(langList,publicText)--Attention, calling this will destory al
end
end
end
local tipMeta={__call=function(L)return L[math.random(#L)]end}
for i=1,#langList do
local L=langList[i]
--Set public text
for _,L in next,langList do
for key,list in next,publicText do L[key]=list end
end
--Set public text
for key,list in next,publicText do
L[key]=list
end
--Fallback to other language, default zh
if i>1 then
_langFallback(langList[L.fallback or 1],L)
end
--Metatable:__call for table:getTip
if type(rawget(L,'getTip'))=='table'then
setmetatable(L.getTip,tipMeta)
--Fallback to default language
for name,L in next,langList do
if name~=defaultLang then
_langFallback(langList[L.fallback or defaultLang],L)
end
end
LANG.init,LANG.setLangList,LANG.setPublicText=nil
--Custom pretreatment for each language
for _,L in next,langList do
pretreatFunc(L)
end
function LANG.set(l)
if text~=langList[l]then
text=langList[l]
WIDGET.setLang(text.WidgetText)
for k,v in next,drawableText do
if text[k]then
v:set(text[k])
end
end
end
function LANG.get(l)
return langList[l]
end
function LANG.addScene(name)
for i=1,#langList do
if langList[i].WidgetText and not langList[i].WidgetText[name]then
langList[i].WidgetText[name]={back=langList[i].back}
for _,L in next,langList do
if L.WidgetText and not L.WidgetText[name]then
L.WidgetText[name]={back=L.back}
end
end
end
function LANG.init()end
end
return LANG