Skip to content

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.

lua
return {
    "helloWorld": "Hello World",
    "goodbyeWorld": "Goodbye World"
}
lua
return {
    "helloWorld": "Bonjour le monde",
    "goodbyeWorld": "Au revoir le monde"
}

Load them in your fxmanifest.lua:

lua
files {
    "locales/*.lua"
}

Done, you're all set!

Configuration Variables (Convars)

ConvarSideTypeDefaultDescription
jo_libs:i18n:allowSwitchLocaleClientStringtrueAllow switching locale
jo_libs:i18n:localeCommandClientStringsetLocaleCommand to switch locale
jo_libs:i18n:localeSharedStringenDefault locale

JO Functions

jo.i18n.__()

Alias __()

Translate a key

Syntax

lua
jo.i18n.__(key)

Parameters

key : string

The key to translate

Return Value

Type : string

The translated key

Example

lua
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

lua
jo.i18n.addEntries(entries)

Parameters

entries : table

The entries to add

Example

lua
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

lua
jo.i18n.addEntry(key, value)

Parameters

key : string

The key to add

value : string

The value to add

Example

lua
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

lua
jo.i18n.findMissingKeys(locale)

Parameters

locale : string

The locale to check

Example

lua
jo.i18n.findMissingKeys("es")
-- Expected output: 'Missing key '<key>' for locale '<locale>'

jo.i18n.getEntries()

Get all entries

Syntax

lua
jo.i18n.getEntries()

Return Value

Type : table

The entries


jo.i18n.getLocale()

Returns the current locale

Syntax

lua
jo.i18n.getLocale()

Return Value

Type : string

The current locale

Example

lua
local locale = jo.i18n.getLocale()
print(locale)
-- Expected output: 'en' (or current locale)

jo.i18n.init()

Initialize the i18n module

Syntax

lua
jo.i18n.init()

Example

lua
jo.i18n.init()

jo.i18n.loadLocale()

Load a locale

Syntax

lua
jo.i18n.loadLocale(locale)

Parameters

locale : string

The locale to load

Example

lua
jo.i18n.loadLocale("es")

jo.i18n.onLocaleChanged()

Register a callback to be called when the locale changes

Syntax

lua
jo.i18n.onLocaleChanged(callback, priority)

Parameters

callback : function

The callback to register

priority : number

The priority of the callback

Example

lua
jo.i18n.onLocaleChanged(function(newLocale)
  print("Locale changed to: " .. newLocale)
end)

jo.i18n.setLocale()

Set the current locale

Syntax

lua
jo.i18n.setLocale(locale)

Parameters

locale : string

The locale to set

Example

lua
jo.i18n.setLocale("es")

Last updated: