From the reference libraryFiveM

Shared

ox/ox_lib/Locale/Shared.mdx

Shared

Allows servers to set a preferred language and attempt to load locale files in any resources using the module. Locale files should use the ISO Language Code and be saved as ./locales/langcode.json

Setup

To change the preferred language from English, add the convar to your server.cfg and change en to the desired language code.

setr ox:locale en

Create a locales directory and a file for your language.

{
  "grand_theft_auto": "grand theft auto",
  "male": "male",
  "female": "female",
  "suspect_sex": "suspect is %s"
}
{
  "grand_theft_auto": "vol de voiture",
  "male": "homme",
  "female": "femme",
  "suspect_sex": "le suspect est %s"
}
files {
  'locales/*.json'
}

Usage

Initialise the locale module in your resource (once).

lib.locale()
import {initLocale} from '@overextended/ox_lib/shared'

initLocale()

Format your strings with the new locale global. Additional arguments can be sent to format the locale output.

locale(str, ...)
import { locale } from '@overextended/ox_lib/shared'

locale(str, ...)
  • str: string
  • vararg?: string or number

<u>Example</u>

-- Load the locale module
lib.locale()

SetInterval(function()
    print(locale('grand_theft_auto'))
    print(locale('suspect_sex', locale('male')))
end, 5000)
```ts
import { initLocale, locale } from '@overextended/ox_lib/shared

// Load the locale module
initLocale()

setInterval(() => {
  console.log(locale('grand_theft_auto'))
  console.log(locale('suspect_sex', locale('male')))
}, 5000)
```

Phrases

You can create a locale string that references other locales to construct a phrase, rather than calling locale multiple times.

{
  "hello": "hello %s",
  "my_name_is": "my name is %s",
  "hello_my_name_is": "${hello}! ${my_name_is}."
}
print(locale('hello_my_name_is', 'doka', 'linden'))
```ts
import { locale } from '@overextended/ox_lib/shared'

console.log(locale('hello_my_name_is', 'doka', 'linden'))
```

lib.getLocale

Gets a locale string from another resource and adds it to the dict.

lib.getLocale(resource, key)
  • resource: string
  • key: string
Back to documentation