|
|
|
@ -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 |
|
|
|
|