mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
feat(locale): add lib.getLocale for sharing locales between resources
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -50,3 +50,5 @@ if not LoadResourceFile(lib.name, 'web/build/index.html') then
|
||||
end
|
||||
|
||||
function lib.hasLoaded() return true end
|
||||
|
||||
lib.locale()
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
lib.locale()
|
||||
Reference in New Issue
Block a user