language模块完全排除外部数据

This commit is contained in:
MrZ626
2020-12-08 20:32:23 +08:00
parent 80eb2e8371
commit e5af9f53e6
4 changed files with 108 additions and 101 deletions

View File

@@ -1,52 +1,3 @@
local langList={
require"parts/language/lang_zh",
require"parts/language/lang_zh2",
require"parts/language/lang_en",
require"parts/language/lang_fr",
require"parts/language/lang_sp",
require"parts/language/lang_symbol",
require"parts/language/lang_yygq",
--Add new language file to LANG folder. Attention, new language won't show in-game when you add language
}
local publicText={
block={
"Z","S","J","L","T","O","I",
"Z5","S5","Q","P","F","E",
"T5","U","V","W","X",
"J5","L5","R","Y","N","H","I5"
},
}
local publicWidgetText={
calculator={
_1="1",_2="2",_3="3",
_4="4",_5="5",_6="6",
_7="7",_8="8",_9="9",
_0="0",["."]=".",e="e",
["+"]="+",["-"]="-",["*"]="*",["/"]="/",
["<"]="<",["="]="=",
play="-->",
},
staff={},
history={
prev="",
next="",
},
lang={
zh="中文",
zh2="全中文",
en="English",
fr="Français",
sp="Español",
symbol="?????",
yygq="就这?",
},
custom_field={
b0="",b1="",b2="",b3="",b4="",b5="",b6="",b7="",
b8="",b9="",b10="",b11="",b12="",b13="",b14="",b15="",b16="",
b17="[ ]",b18="N",b19="B",b20="_",b21="_",b22="_",b23="_",b24="_",
},
mg_cubefield={},
}
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
@@ -58,43 +9,53 @@ local function langFallback(T0,T)
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 key,list in next,publicText do
L[key]=list
end
--Set public widget text
for key,list in next,publicWidgetText do
local WT=L.WidgetText
if not WT[key]then WT[key]={}end
for k,v in next,list do
WT[key][k]=v
end
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(L.getTip)=="table"then
setmetatable(L.getTip,tipMeta)
end
--set global name for all back button
for _,v in next,L.WidgetText do
v.back=L.back
end
end
local langList={}
local publicText,publicWidgetText={},{}
local LANG={}
function LANG.getLen()
return #langList
--Must call before LANG.init
function LANG.setLangList(list)langList=list end
function LANG.setPublicText(L)publicText=L end
function LANG.setPublicWidgetText(L)publicWidgetText=L end
function LANG.init()
for i=1,#langList do
local L=langList[i]
--Set public text
for key,list in next,publicText do
L[key]=list
end
--Set public widget text
for key,list in next,publicWidgetText do
local WT=L.WidgetText
if not WT[key]then WT[key]={}end
for k,v in next,list do
WT[key][k]=v
end
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)
end
--set global name for all back button
for _,v in next,L.WidgetText do
v.back=L.back
end
end
LANG.init=nil
end
function LANG.set(l)
text=langList[l]
WIDGET.setLang(text.WidgetText)
@@ -102,4 +63,5 @@ function LANG.set(l)
drawableText[s]:set(text[s])
end
end
return LANG

View File

@@ -39,6 +39,8 @@ local SCN={
function SCN.add(name,scene)
scenes[name]=scene
if not scene.widgetList then scene.widgetList={}end
setmetatable(scene.widgetList,WIDGET.indexMeta)
end
function SCN.swapUpdate()
@@ -76,7 +78,7 @@ function SCN.init(s,org)
SCN.gamepadUp=S.gamepadUp
SCN.socketRead=S.socketRead
if S.sceneInit then S.sceneInit(org)end
WIDGET.set(s)
WIDGET.set(S.widgetList)
end
function SCN.push(tar,style)
if not SCN.swapping then

View File

@@ -810,7 +810,7 @@ function WIDGET.lnk_pressKey(k) return function() love.keypressed(k) end end
function WIDGET.lnk_goScene(t,s) return function() SCN.go(t,s) end end
function WIDGET.lnk_swapScene(t,s) return function() SCN.swapTo(t,s) end end
local indexMeta={
WIDGET.indexMeta={
__index=function(L,k)
for i=1,#L do
if L[i].name==k then
@@ -819,16 +819,7 @@ local indexMeta={
end
end
}
function WIDGET.init(sceneName,list)
local L={}
for i=1,#list do
ins(L,list[i])
end
setmetatable(L,indexMeta)
widgetList[sceneName]=L
end
function WIDGET.set(sceneName)
local list=widgetList[sceneName]
function WIDGET.set(list)
kb.setTextInput(false)
WIDGET.sel=nil
WIDGET.active=list or NONE
@@ -840,10 +831,12 @@ function WIDGET.set(sceneName)
end
end
end
function WIDGET.setLang(lang)
for S,L in next,widgetList do
for _,W in next,L do
W.text=lang[S][W.name]
function WIDGET.setLang(widgetText)
for S,L in next,SCN.scenes do
if widgetText[S]then
for _,W in next,L.widgetList do
W.text=widgetText[S][W.name]
end
end
end
end