From fdb330a38fbbf69b13eecccc0de484f63a963377 Mon Sep 17 00:00:00 2001 From: Luke Date: Sun, 1 May 2022 10:31:13 +0200 Subject: [PATCH] fix(client): Move loadLocale function out of init Setting a locale causes the whole application to be rerendered, meaning that the fetch for init would also run every time when locale is set --- resource/locale/client.lua | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/resource/locale/client.lua b/resource/locale/client.lua index f6731e3..ca50ecb 100644 --- a/resource/locale/client.lua +++ b/resource/locale/client.lua @@ -1,9 +1,12 @@ local default = { 'en', 'fr', 'de', 'it', 'es', 'pt-BR', 'pl', 'ru', 'ko', 'zh-TW', 'ja', 'es-MX', 'zh-CN' } +local uiLoaded = false local userLocale = GetResourceKvpString('locale') local function loadLocale(locale, cb) if cb then cb(1) end + if not locale then locale = userLocale end local JSON = LoadResourceFile('ox_lib', ('locales/%s.json'):format(locale)) or LoadResourceFile('ox_lib', ('locales/en.json'):format(locale)) + -- if cb then cb(JSON) end SendNUIMessage({ action = 'setLocale', data = json.decode(JSON) @@ -19,10 +22,10 @@ RegisterNUICallback('closeSettings', function(_, cb) SetNuiFocus(false, false) end) -if not userLocale then userLocale = default[GetCurrentLanguage() + 1] end -RegisterNUICallback('init', function(_, cb) - cb(1) +Citizen.CreateThread(function() + if not userLocale then userLocale = default[GetCurrentLanguage() + 1] end + while not uiLoaded do Wait(0) end loadLocale(userLocale) SendNUIMessage({ -- Loads the `default` array into select options action = 'loadLocales', @@ -30,6 +33,12 @@ RegisterNUICallback('init', function(_, cb) }) end) + +RegisterNUICallback('init', function(_, cb) + cb(1) + uiLoaded = true +end) + RegisterCommand('ox_lib', function() SendNUIMessage({ action = 'openSettings',