replaced Locale with Ace3 style

CiscOH Bellic 2 years ago
parent defe1b8d94
commit 93fda2da61
  1. 10
      scripts/Locales/enUS.lua
  2. 7
      scripts/Locales/enUS/enUS.lua
  3. 95
      src/Locale/Locale.lua
  4. 5
      src/_bastion.lua

@ -1,10 +0,0 @@
local Tinkr, Bastion = ...
if not Bastion.Locale.ShouldLoad("enUS") then return end
local L = {}
L["Tinkr"] = "Tinkr"
L["Bastion"] = "Bastion"
Bastion.Locale.SetTable(L)

@ -0,0 +1,7 @@
local Tinkr, Bastion = ...
local L = Bastion.Locale:NewLocale("Bastion", "enUS", true)
L["Tinkr"] = true
L["Bastion"] = true

@ -4,38 +4,89 @@ local Locale = {}
Locale.__index = Locale Locale.__index = Locale
local private = { local private = {
gameLocale = GetLocale(),
locale = nil, locale = nil,
tbl = nil, tbl = nil,
hasNoLocaleTable = false hasNoLocaleTable = false
} }
private.locale = GetLocale() if private.gameLocale == "enGB" then
private.gameLocale = "enUS"
function Locale.GetTable()
assert(private.tbl)
return private.tbl
end end
function Locale.ShouldLoad(locale) Locale.apps = {}
assert(private.locale) Locale.appnames = {}
return locale == private.locale
local readmeta = {
__index = function(self, key) -- requesting totally unknown entries: fire off a nonbreaking error and return key
rawset(self, key, key) -- only need to see the warning once, really
Bastion:Debug("Bastion Locale: " .. tostring(Locale.appnames[self]) .. ": Missing entry for '"..tostring(key).."'")
return key
end
}
-- This metatable is used on all tables returned from GetLocale if the silent flag is true, it does not issue a warning on unknown keys
local readmetasilent = {
__index = function(self, key) -- requesting totally unknown entries: return key
rawset(self, key, key) -- only need to invoke this function once
return key
end
}
local registering
local assertfalse = function() assert(false) end
local writeproxy = setmetatable({}, {
__newindex = function(self, key, value)
rawset(registering, key, value == true and key or value)
end,
__index = assertfalse
})
local writedefaultproxy = setmetatable({}, {
__newindex = function(self, key, value)
rawset(registering, key, value == true and key or value)
end,
__index = assertfalse
})
function Locale:NewLocale(application, locale, isDefault, silent)
local activeGameLocale = GAME_LOCALE or private.gameLocale
local app = Locale.apps[application]
if silent and app and getmetatable(app) ~= readmetasilent then
Bastion:Debug("Usage: NewLocale(locale[, isDefault[, silent]]): 'silent' must be specified for the first locale registered")
end
if not app then
if silent == "raw" then
app = {}
else
app = setmetatable({}, silent and readmetasilent or readmeta)
end
Locale.apps[application] = app
Locale.appnames[app] = application
end
if locale ~= activeGameLocale and not isDefault then
return
end
registering = app
if isDefault then
return writedefaultproxy
else
return writeproxy
end
end end
function Locale.SetTable(tbl) function Locale:GetLocale(application, silent)
private.tbl = setmetatable(tbl, { if not silent and not Locale.apps[application] then
__index = function(t, k) Bastion:Debug("Usage: GetLocale(application[, silent]): 'application' - No locales registered for '"..tostring(application).."'")
local v = tostring(k) end
if not private.hasNoLocaleTable then return Locale.apps[application] or {}
error(string.format("Locale string does not exist: \"%s\"", v))
end
rawset(t, k, v)
return v
end,
__newindex = function()
error("Cannot write to the locale table")
end,
})
end end
return Locale return Locale

@ -61,10 +61,9 @@ Bastion.Globals = {}
---@type Locale ---@type Locale
Bastion.Locale = Bastion.require("Locale") Bastion.Locale = Bastion.require("Locale")
Bastion:Require("~/scripts/Locales/enUS/enUS")
Load("@Locales/") Load("@Locales/")
-- Bastion.Locales = {} local L = Bastion.Locale:GetLocale("Bastion", true)
-- Bastion.Locales.enUS = Bastion:Require("@Locales/enUS")
local L = Bastion.Locale.GetTable()
Bastion.Globals.L = L Bastion.Globals.L = L
---@type Environment ---@type Environment
Bastion.Environment = Bastion.require("Environment") Bastion.Environment = Bastion.require("Environment")

Loading…
Cancel
Save