mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 15:36:02 +01:00
feat: locale system
This commit is contained in:
@@ -40,7 +40,8 @@ files {
|
|||||||
'imports/**/client.lua',
|
'imports/**/client.lua',
|
||||||
'imports/**/shared.lua',
|
'imports/**/shared.lua',
|
||||||
'web/build/index.html',
|
'web/build/index.html',
|
||||||
'web/build/**/*'
|
'web/build/**/*',
|
||||||
|
'locales/*.json',
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_script 'resource/main.lua'
|
shared_script 'resource/main.lua'
|
||||||
|
|||||||
30
imports/locale/shared.lua
Normal file
30
imports/locale/shared.lua
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
local dict
|
||||||
|
|
||||||
|
function locale(str, ...)
|
||||||
|
local lstr = dict[str]
|
||||||
|
|
||||||
|
if lstr then
|
||||||
|
if ... then
|
||||||
|
return lstr and lstr:format(...)
|
||||||
|
end
|
||||||
|
|
||||||
|
return lstr
|
||||||
|
end
|
||||||
|
|
||||||
|
return ("Translation for '%s' does not exist"):format(str)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function loadLocale(locale)
|
||||||
|
local resourceName = GetCurrentResourceName()
|
||||||
|
local JSON = LoadResourceFile(resourceName, ('locales/%s.json'):format(locale)) or LoadResourceFile(resourceName, ('locales/en-US.json'):format(locale))
|
||||||
|
|
||||||
|
if JSON then
|
||||||
|
dict = json.decode(JSON)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
AddEventHandler('ox_lib:setLocale', loadLocale)
|
||||||
|
|
||||||
|
return function()
|
||||||
|
loadLocale(GetExternalKvpString('ox_lib', 'locale') or 'en-US')
|
||||||
|
end
|
||||||
24
resource/locale/client.lua
Normal file
24
resource/locale/client.lua
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
local userLocale = GetResourceKvpString('locale')
|
||||||
|
|
||||||
|
local function loadLocale(locale, cb)
|
||||||
|
local JSON = LoadResourceFile('ox_lib', ('locales/%s.json'):format(locale)) or LoadResourceFile('ox_lib', ('locales/en-US.json'):format(locale))
|
||||||
|
if cb then cb(JSON) end
|
||||||
|
|
||||||
|
SetResourceKvp('locale', locale)
|
||||||
|
TriggerEvent('ox_lib:setLocale', locale)
|
||||||
|
end
|
||||||
|
|
||||||
|
RegisterNUICallback('getLocale', loadLocale)
|
||||||
|
|
||||||
|
if not userLocale then
|
||||||
|
local default = { 'en-US', 'fr-FR', 'de-DE', 'it-IT', 'es-ES', 'pt-BR', 'pl-PL', 'ru-RU', 'ko-KR', 'zh-TW', 'ja-JP', 'es-MX', 'zh-CN' }
|
||||||
|
userLocale = default[GetCurrentLanguage() + 1]
|
||||||
|
end
|
||||||
|
|
||||||
|
loadLocale(userLocale)
|
||||||
|
|
||||||
|
RegisterCommand('setlocale', function(_, args, _)
|
||||||
|
if args?[1] then
|
||||||
|
loadLocale(args[1])
|
||||||
|
end
|
||||||
|
end, false)
|
||||||
5
resource/locale/server.lua
Normal file
5
resource/locale/server.lua
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
RegisterCommand('serverlocale', function(_, args, _)
|
||||||
|
if args?[1] then
|
||||||
|
SetResourceKvp('locale', args[1])
|
||||||
|
end
|
||||||
|
end, true)
|
||||||
Reference in New Issue
Block a user