diff --git a/imports/locale/shared.lua b/imports/locale/shared.lua index 9a221b6..f051dc2 100644 --- a/imports/locale/shared.lua +++ b/imports/locale/shared.lua @@ -37,6 +37,7 @@ function lib.getLocales() return dict end +---Loads the ox_lib locale module. Prefer using fxmanifest instead (see [docs](https://overextended.dev/ox_lib#usage)). function lib.locale() local lang = GetConvar('ox:locale', 'en') local locales = json.decode(LoadResourceFile(cache.resource, ('locales/%s.json'):format(lang))) @@ -75,4 +76,32 @@ function lib.locale() end end +---Gets a locale string from another resource and adds it to the dict. +---@param resource string +---@param key string +---@return string? +function lib.getLocale(resource, key) + local locale = dict[key] + + if locale then + warn(("overwriting existing locale '%s' (%s)"):format(key, locale)) + end + + locale = exports[resource]:getLocale(key) + dict[key] = locale + + if not locale then + warn(("no locale exists with key '%s' in resource '%s'"):format(key, resource)) + end + + return locale +end + +---Backing function for lib.getLocale. +---@param key string +---@return string? +exports('getLocale', function(key) + return dict[key] +end) + return lib.locale diff --git a/resource/init.lua b/resource/init.lua index a9869eb..9eb7ba8 100644 --- a/resource/init.lua +++ b/resource/init.lua @@ -50,3 +50,5 @@ if not LoadResourceFile(lib.name, 'web/build/index.html') then end function lib.hasLoaded() return true end + +lib.locale() diff --git a/resource/shared.lua b/resource/shared.lua deleted file mode 100644 index aa9ba61..0000000 --- a/resource/shared.lua +++ /dev/null @@ -1 +0,0 @@ -lib.locale()