Add failed message, default lang

This commit is contained in:
Qwreey 2023-02-17 10:45:11 +09:00
parent 7361c012db
commit 11c32680ef
3 changed files with 70 additions and 41 deletions

View file

@ -12,6 +12,7 @@ function module.init(shared)
local PcallGetProperty = class.PcallGetProperty local PcallGetProperty = class.PcallGetProperty
new.Locales = { new.Locales = {
["Default"] = "default";
["English"] = "en_us"; ["English"] = "en_us";
["Spanish"] = "es_es"; ["Spanish"] = "es_es";
["French"] = "fr_fr"; ["French"] = "fr_fr";
@ -59,14 +60,17 @@ function module.init(shared)
["Swedish"] = "sv_se"; ["Swedish"] = "sv_se";
["Arabic"] = "ar_001"; ["Arabic"] = "ar_001";
} }
new.Lang = game:GetService("LocalizationService").RobloxLocaleId new.CurrentLocale = game:GetService("LocalizationService").RobloxLocaleId
new.Default = new.Locales.English new.FailedMessage = "<Localization Failed>"
local lang = {} local lang = {}
lang.__index = lang lang.__index = lang
function lang.New(id,handlers) function lang.New(id,handlers)
local this = {handlers=handlers,id=id,store=store.New(),index=1} local this = {handlers=handlers,id=id,store=store.New(),index=1}
lang[id] = this lang[id] = this
if not handlers[new.Locales.Default] then
warn(("Localization %s have no Default value, Did you missed '[Lang.Locales.Default] = ...' ? It may make Localization fail on other languages"))
end
setmetatable(this,lang) setmetatable(this,lang)
end end
@ -84,17 +88,7 @@ function module.init(shared)
local optionType = type(option) local optionType = type(option)
local formatted local formatted
if quadType == "quad_register" then if quadType == "quad_register" then
local default = option.dvalue formatted = option:CalcWithDefault(nil)
local with = option.wfunc
local tstore = option.store
local rawKey = option.key
local value = tstore[rawKey]
if value == nil then
value = default
elseif with then
value = with(tstore,value,rawKey)
end
formatted = value
elseif optionType == "number" then elseif optionType == "number" then
if option%1 == 0 then if option%1 == 0 then
-- int -- int
@ -119,12 +113,24 @@ function module.init(shared)
end end
local Update = lang.Update local Update = lang.Update
function lang:Format(options) function lang:UpdateAll()
-- find handler -- find handler
local handler = self.handlers[new.Lang] local handler = self.handlers[new.Lang]
if not handler then if not handler then
handler = self.handlers[new.Default] handler = self.handlers[new.Default]
end end
for index = 1,self.index do
Update(self.store,handler,index,options)
end
end
function lang:Format(options)
-- find handler
local handler = self.handlers[new.Lang]
if not handler then
handler = self.handlers[new.Locales.Default]
end
if not handler then if not handler then
warn(("Langauge handle %s is not found on Localization %s"):format(new.Default,self.id)) warn(("Langauge handle %s is not found on Localization %s"):format(new.Default,self.id))
end end
@ -132,24 +138,46 @@ function module.init(shared)
self.index = index + 1 self.index = index + 1
-- setup updater -- setup updater
local registerKeep = {}
-- this table will destroyed when parent register was destroyed
-- that means, if some other registerFN was inserted in registerKeep,
-- it will be destroyed when unnecessary
-- maybe better then self.store.__keep for GC
-- it will be not destroyed. anyway; registerKeep will be connected
-- to real instance OR local
for _,option in pairs(options) do for _,option in pairs(options) do
local quadType = PcallGetProperty(option,"__type") local quadType = PcallGetProperty(option,"__type")
-- if it is register, setup updater -- if it is register, setup updater
if quadType == "quad_register" then if quadType == "quad_register" then
local function regFn() local function regFn()
-- !HOLD IT SELF TO PREVENT THIS REGISTER BEGIN REMOVED FROM MEMORY -- update handler
handler = self.handlers[new.Lang]
if not handler then
handler = self.handlers[new.Locales.Default]
end
if not handler then
self.store[tostring(index)] = new.FailedMessage
end
Update(self.store,handler,index,options) Update(self.store,handler,index,options)
-- !HOLD IT SELF TO PREVENT THIS REGISTER BEGIN REMOVED FROM MEMORY
end end
option:Register(regFn) option:Register(regFn)
insert(self.store.__keep,regFn) insert(registerKeep,regFn)
end end
end end
-- update once -- update once
Update(self.store,handler,index,options) if handler then
Update(self.store,handler,index,options)
else
self.store[tostring(index)] = new.FailedMessage
end
-- return resiter -- return resiter
return self.store(tostring(index)) local register = self.store(tostring(index))
register.registerKeep = registerKeep
register.__rtype = "LangUpdater"
return register
end end
function new.New(id,handlers) function new.New(id,handlers)

View file

@ -188,7 +188,7 @@ function module.init(shared)
warn "[Quad] no default value found." warn "[Quad] no default value found."
end end
end; end;
CalcWithNewValue = function(s,withItem,newValue,key) CalcWithNewValue = function(s,withItem,updatedValue,updatedKey)
local with = s.wfunc local with = s.wfunc
local tstore = s.store local tstore = s.store
local rawKey = s.key local rawKey = s.key
@ -196,15 +196,15 @@ function module.init(shared)
local from = s.fvalue local from = s.fvalue
local add = s.avalue local add = s.avalue
if add then if add then
newValue = newValue + add updatedValue = updatedValue + add
end end
if from then if from then
newValue = from[newValue] updatedValue = from[updatedValue]
end end
if with then if with then
newValue = with(tstore,newValue,key,withItem) updatedValue = with(tstore,updatedValue,updatedKey,withItem)
end end
return newValue,tween return updatedValue,tween
end end
} }
registerClass.__index = registerClass registerClass.__index = registerClass
@ -261,7 +261,7 @@ function module.init(shared)
end end
end end
end end
-- init bindings -- init bindings (reflect all changes into class or other store, just wrapping it)
function new.__initStoreRegisterBinding(self,withItem) function new.__initStoreRegisterBinding(self,withItem)
local selfTweens = self.__tweens local selfTweens = self.__tweens
local selfValues = self.__self local selfValues = self.__self

View file

@ -28,7 +28,7 @@ Style "tweenTestFrame" {
Lang.New("tweenCount",{ Lang.New("tweenCount",{
[Lang.Locales.Korean] = "{count}번 실행했어요"; [Lang.Locales.Korean] = "{count}번 실행했어요";
[Lang.Locales.English] = "Ran {count} times"; [Lang.Locales.Default] = "Ran {count} times";
}) })
-- Create GUI -- Create GUI
@ -52,7 +52,7 @@ function tweenTest:Render(Prop)
TextLabel { TextLabel {
Size = UDim2.new(1,0,0,30); Size = UDim2.new(1,0,0,30);
Text = Lang "tweenCount" { Text = Lang "tweenCount" {
count = Prop "Count":With(function(_,value) return value*2 end); count = Prop "Count";
}; };
}; };
Size = UDim2.fromScale(1,1); Size = UDim2.fromScale(1,1);
@ -108,20 +108,21 @@ function tweenTest:Render(Prop)
Text = "Back"; Text = "Back";
Position = Prop "Position":Add(UDim2.fromOffset(0,560)):Tween{Direction = "InOut",Time = 2,Easing = "Back"}; Position = Prop "Position":Add(UDim2.fromOffset(0,560)):Tween{Direction = "InOut",Time = 2,Easing = "Back"};
}; };
-- frame { Frame {
-- Size = UDim2.new(100,100); Size = UDim2.new(100,100);
-- [event.created] = function(self) [Event.Created] = function(self)
-- for i = 1,100 do for j = 1,25 do
-- mount(self,frame{ local i = j*4
-- Size = UDim2.fromOffset(3,3); Mount(self,Frame{
-- AnchorPoint = Vector2.new(0.5,0.5); Size = UDim2.fromOffset(3,3);
-- Position = UDim2.fromOffset(100-i,tween.CalcEasing(tween.EasingFunctions.Circle,tween.EasingDirections.InOut,i/100)*100); AnchorPoint = Vector2.new(0.5,0.5);
-- BackgroundColor3 = Color3.fromRGB(110, 110, 255); Position = UDim2.fromOffset(100-i,Tween.CalcEasing(Tween.EasingFunctions.Circle,Tween.EasingDirections.InOut,i/100)*100);
-- }) BackgroundColor3 = Color3.fromRGB(110, 110, 255);
-- end })
-- end; end
-- BackgroundColor3 = Color3.fromRGB(0,0,0); end;
-- }; BackgroundColor3 = Color3.fromRGB(0,0,0);
};
} }
end end