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
new.Locales = {
["Default"] = "default";
["English"] = "en_us";
["Spanish"] = "es_es";
["French"] = "fr_fr";
@ -59,14 +60,17 @@ function module.init(shared)
["Swedish"] = "sv_se";
["Arabic"] = "ar_001";
}
new.Lang = game:GetService("LocalizationService").RobloxLocaleId
new.Default = new.Locales.English
new.CurrentLocale = game:GetService("LocalizationService").RobloxLocaleId
new.FailedMessage = "<Localization Failed>"
local lang = {}
lang.__index = lang
function lang.New(id,handlers)
local this = {handlers=handlers,id=id,store=store.New(),index=1}
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)
end
@ -84,17 +88,7 @@ function module.init(shared)
local optionType = type(option)
local formatted
if quadType == "quad_register" then
local default = option.dvalue
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
formatted = option:CalcWithDefault(nil)
elseif optionType == "number" then
if option%1 == 0 then
-- int
@ -119,12 +113,24 @@ function module.init(shared)
end
local Update = lang.Update
function lang:Format(options)
function lang:UpdateAll()
-- find handler
local handler = self.handlers[new.Lang]
if not handler then
handler = self.handlers[new.Default]
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
warn(("Langauge handle %s is not found on Localization %s"):format(new.Default,self.id))
end
@ -132,24 +138,46 @@ function module.init(shared)
self.index = index + 1
-- 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
local quadType = PcallGetProperty(option,"__type")
-- if it is register, setup updater
if quadType == "quad_register" then
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)
-- !HOLD IT SELF TO PREVENT THIS REGISTER BEGIN REMOVED FROM MEMORY
end
option:Register(regFn)
insert(self.store.__keep,regFn)
insert(registerKeep,regFn)
end
end
-- update once
if handler then
Update(self.store,handler,index,options)
else
self.store[tostring(index)] = new.FailedMessage
end
-- return resiter
return self.store(tostring(index))
local register = self.store(tostring(index))
register.registerKeep = registerKeep
register.__rtype = "LangUpdater"
return register
end
function new.New(id,handlers)

View file

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

View file

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