forked from Simnation/Main
53 lines
1.6 KiB
Lua
53 lines
1.6 KiB
Lua
local localization = {}
|
|
|
|
---Returns the the text associated with the key in the language set in the config
|
|
---@param key string
|
|
---@param ... any
|
|
---@return string
|
|
function GetLocalization(key, ...)
|
|
local locale = localization[key]
|
|
if not locale then
|
|
return tostring(key)
|
|
end
|
|
|
|
if ... ~= nil then
|
|
return string.format(locale, ...)
|
|
else
|
|
return locale
|
|
end
|
|
end
|
|
|
|
---Attempts to load the spesifed localization file
|
|
---@param langCode string
|
|
---@return boolean success
|
|
local function loadLocalizationFile(langCode)
|
|
local jsonFile = LoadResourceFile(CURRENT_RESOURCE, string.format('locales/%s.json', langCode))
|
|
if jsonFile == nil then
|
|
return false
|
|
end
|
|
|
|
local locales = json.decode(jsonFile)
|
|
if locales == nil then
|
|
return false
|
|
end
|
|
|
|
localization = locales
|
|
return true
|
|
end
|
|
|
|
---Loads the translations into a lua table for later use
|
|
local function loadTranslations()
|
|
local success = loadLocalizationFile(Config.Language)
|
|
if success then
|
|
return
|
|
end
|
|
|
|
local loadedDefault = loadLocalizationFile('en')
|
|
if loadedDefault then
|
|
warn(string.format('The selected language "%s" (%s.json) was not able to load! en.json was loaded instead! (does the file exist in the locales folder?)', tostring(Config.Language), tostring(Config.Language)))
|
|
else
|
|
error(string.format('Neither the selected language "%s" (%s.json) nor en.json was able to load! This will cause major issues.', tostring(Config.Language), tostring(Config.Language)))
|
|
end
|
|
end
|
|
|
|
loadTranslations()
|