i18n Shared
i18n module is a specialized tool for internationalization within RedM.
Setup
Create a locales folder in your resource and add your locale files there.
return {
"helloWorld": "Hello World",
"goodbyeWorld": "Goodbye World"
}return {
"helloWorld": "Bonjour le monde",
"goodbyeWorld": "Au revoir le monde"
}Load them in your fxmanifest.lua:
files {
"locales/*.lua"
}Done, you're all set!
Configuration Variables (Convars)
| Convar | Side | Type | Default | Description |
|---|---|---|---|---|
jo_libs:i18n:allowSwitchLocale | Client | String | true | Allow switching locale |
jo_libs:i18n:localeCommand | Client | String | setLocale | Command to switch locale |
jo_libs:i18n:locale | Shared | String | en | Default locale |
JO Functions
jo.i18n.__()
Alias __()
Translate a key
Syntax
jo.i18n.__(key)Parameters
key : string
The key to translate
Return Value
Type : string
The translated key
Example
local text = jo.i18n.__("helloWorld")
-- OR --
local text = __("helloWorld")
print(text) -- "Hello World" (or translated text)jo.i18n.addEntries()
Add multiple entries to the i18n dictionary
The function is exported to allow modification for other resources
Syntax
jo.i18n.addEntries(entries)Parameters
entries : table
The entries to add
Example
jo.i18n.addEntries({
"buy" = "Buy",
"sell" = "Sell"
})
-- OR --
exports.<scriptName>:addEntries({
"buy" = "Buy",
"sell" = "Sell"
})jo.i18n.addEntry()
Add a single entry to the i18n dictionary
The function is exported to allow modification for other resources
Syntax
jo.i18n.addEntry(key, value)Parameters
key : string
The key to add
value : string
The value to add
Example
jo.i18n.addEntry("buyHorse", "Buy a horse")
-- OR --
exports.<scriptName>:addEntry("buyHorse", "Buy a horse")jo.i18n.findMissingKeys()
Find missing keys in a locale
Syntax
jo.i18n.findMissingKeys(locale)Parameters
locale : string
The locale to check
Example
jo.i18n.findMissingKeys("es")
-- Expected output: 'Missing key '<key>' for locale '<locale>'jo.i18n.getEntries()
Get all entries
Syntax
jo.i18n.getEntries()Return Value
Type : table
The entries
jo.i18n.getLocale()
Returns the current locale
Syntax
jo.i18n.getLocale()Return Value
Type : string
The current locale
Example
local locale = jo.i18n.getLocale()
print(locale)
-- Expected output: 'en' (or current locale)jo.i18n.init()
Initialize the i18n module
Syntax
jo.i18n.init()Example
jo.i18n.init()jo.i18n.loadLocale()
Load a locale
Syntax
jo.i18n.loadLocale(locale)Parameters
locale : string
The locale to load
Example
jo.i18n.loadLocale("es")jo.i18n.onLocaleChanged()
Register a callback to be called when the locale changes
Syntax
jo.i18n.onLocaleChanged(callback, priority)Parameters
callback : function
The callback to register
priority : number
The priority of the callback
Example
jo.i18n.onLocaleChanged(function(newLocale)
print("Locale changed to: " .. newLocale)
end)jo.i18n.setLocale()
Set the current locale
Syntax
jo.i18n.setLocale(locale)Parameters
locale : string
The locale to set
Example
jo.i18n.setLocale("es")