mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
refactor(locale): simplify
BREAKING CHANGE (kind of) Remove user-selection for locales. Adds mostly unnecessary complexity, and most people don't want to bother with it anyway. Locale is always based on the convar "ox:locale" instead.
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
local dict
|
||||
|
||||
---@param str string
|
||||
---@param ... string | number
|
||||
---@return string
|
||||
function locale(str, ...)
|
||||
local lstr = dict[str]
|
||||
|
||||
@@ -14,16 +17,15 @@ function locale(str, ...)
|
||||
return ("Translation for '%s' does not exist"):format(str)
|
||||
end
|
||||
|
||||
function lib.loadLocale(locale)
|
||||
if not locale then
|
||||
locale = lib.context == 'server' and lib.getServerLocale() or GetExternalKvpString('ox_lib', 'locale') or 'en'
|
||||
end
|
||||
---@return { [string]: string }
|
||||
function lib.getLocales()
|
||||
return dict
|
||||
end
|
||||
|
||||
local resourceName = GetCurrentResourceName()
|
||||
local JSON = LoadResourceFile(resourceName, ('locales/%s.json'):format(locale)) or LoadResourceFile(resourceName, ('locales/en.json'):format(locale))
|
||||
function lib.loadLocale()
|
||||
local locale = GetConvar('ox:locale', 'en')
|
||||
local JSON = LoadResourceFile(cache.resource, ('locales/%s.json'):format(locale)) or LoadResourceFile(cache.resource, ('locales/en.json'):format(locale))
|
||||
dict = JSON and json.decode(JSON) or {}
|
||||
end
|
||||
|
||||
AddEventHandler('ox_lib:setLocale', lib.loadLocale)
|
||||
|
||||
return lib.loadLocale
|
||||
return lib.loadLocale
|
||||
|
||||
@@ -1,47 +1,9 @@
|
||||
local default = { 'en', 'fr', 'de', 'it', 'es', 'pt-BR', 'pl', 'ru', 'ko', 'zh-TW', 'ja', 'es-MX', 'zh-CN' }
|
||||
local userLocale = GetResourceKvpString('locale') or default[GetCurrentLanguage() + 1]
|
||||
|
||||
local function loadLocale(locale, cb)
|
||||
if cb then cb(1) end
|
||||
local JSON = LoadResourceFile('ox_lib', ('locales/%s.json'):format(locale)) or LoadResourceFile('ox_lib', ('locales/en.json'):format(locale))
|
||||
|
||||
SendNUIMessage({
|
||||
action = 'setLocale',
|
||||
data = json.decode(JSON)
|
||||
})
|
||||
|
||||
SetResourceKvp('locale', locale)
|
||||
TriggerEvent('ox_lib:setLocale', locale)
|
||||
end
|
||||
|
||||
RegisterNUICallback('getLocale', loadLocale)
|
||||
|
||||
RegisterNUICallback('closeSettings', function(_, cb)
|
||||
cb(1)
|
||||
SetNuiFocus(false, false)
|
||||
end)
|
||||
|
||||
---@todo remove module and nui event
|
||||
RegisterNUICallback('init', function(_, cb)
|
||||
cb(1)
|
||||
cb(1)
|
||||
|
||||
SendNUIMessage({
|
||||
action = 'loadLocales',
|
||||
data = GlobalState.locales
|
||||
})
|
||||
|
||||
loadLocale(userLocale)
|
||||
SendNUIMessage({
|
||||
action = 'loadLocales',
|
||||
data = {}
|
||||
})
|
||||
end)
|
||||
|
||||
RegisterCommand('ox_lib', function()
|
||||
SendNUIMessage({
|
||||
action = 'openSettings',
|
||||
data = GetResourceKvpString('locale') -- Sets the value for language select
|
||||
})
|
||||
SetNuiFocus(true, true)
|
||||
end)
|
||||
|
||||
RegisterCommand('setlocale', function(_, args, _)
|
||||
if args?[1] then
|
||||
loadLocale(args[1])
|
||||
end
|
||||
end, false)
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
do
|
||||
local locales = {}
|
||||
local system = os.getenv('OS')
|
||||
local command = system and system:match('Windows') and 'dir "' or 'ls "'
|
||||
local path = GetResourcePath(lib.name)
|
||||
local types = path:gsub('//', '/') .. '/locales'
|
||||
local suffix = command == 'dir "' and '/" /b' or '/"'
|
||||
local dir = io.popen(command .. types .. suffix)
|
||||
|
||||
if dir then
|
||||
for line in dir:lines() do
|
||||
local file = line:gsub('([%a%-]+).json', '%1')
|
||||
|
||||
if file then
|
||||
locales[#locales+1] = file
|
||||
end
|
||||
end
|
||||
dir:close()
|
||||
end
|
||||
|
||||
GlobalState.locales = locales
|
||||
end
|
||||
|
||||
RegisterCommand('serverlocale', function(_, args, _)
|
||||
if args?[1] then
|
||||
SetResourceKvp('locale', args[1])
|
||||
TriggerEvent('ox_lib:setLocale', args[1])
|
||||
end
|
||||
end, true)
|
||||
|
||||
function lib.getServerLocale()
|
||||
return GetResourceKvpString('locale') or 'en'
|
||||
end
|
||||
Reference in New Issue
Block a user