feat(locale): add lib.getLocale for sharing locales between resources

This commit is contained in:
Linden
2023-12-09 19:28:01 +11:00
parent 048edfa86f
commit 80a2a7c39d
3 changed files with 31 additions and 1 deletions

View File

@@ -37,6 +37,7 @@ function lib.getLocales()
return dict return dict
end end
---Loads the ox_lib locale module. Prefer using fxmanifest instead (see [docs](https://overextended.dev/ox_lib#usage)).
function lib.locale() function lib.locale()
local lang = GetConvar('ox:locale', 'en') local lang = GetConvar('ox:locale', 'en')
local locales = json.decode(LoadResourceFile(cache.resource, ('locales/%s.json'):format(lang))) local locales = json.decode(LoadResourceFile(cache.resource, ('locales/%s.json'):format(lang)))
@@ -75,4 +76,32 @@ function lib.locale()
end end
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 return lib.locale

View File

@@ -50,3 +50,5 @@ if not LoadResourceFile(lib.name, 'web/build/index.html') then
end end
function lib.hasLoaded() return true end function lib.hasLoaded() return true end
lib.locale()

View File

@@ -1 +0,0 @@
lib.locale()