Internationalization (i18n) Library

The i18n library provides comprehensive internationalization support for your RedM framework, allowing you to manage multiple languages and translations across your resources.

Overview

The i18n system supports:

  • Multi-language translation management

  • Server and client-side operation

  • Automatic language detection

  • KVP (Key-Value Pair) storage for persistence

  • Dynamic language switching

  • Parameterized translations

Global Variables

  • lang: Current active language (default: 'en')

  • languageCache: Cached translation data

  • avalLangs: Array of available languages

  • IS_SERVER: Boolean indicating server context

  • RESOURCE_NAME: Current resource name

Core Functions

i18n.getCurrentLanguage

Returns the currently active language code.

Returns: String representing the current language

Example:

i18n.setLang

Changes the active language.

Parameters:

  • language (string): Language code to switch to

Example:

i18n.importData

Imports translation data for a specific language.

Parameters:

  • language (string): Language code

  • translationData (table): Translation data structure

Translation Data Structure:

Example:

i18n.exportData

Exports all cached translation data.

Returns: Table containing all language data

Example:

i18n.translate

Translates a key to the current language with optional parameter formatting.

Parameters:

  • key (string): Translation key in dot notation (prefix.label)

  • ... (variable): Parameters for string formatting

Returns: Translated string

Key Formats:

  • "prefix.label" - Nested key access

  • "simple_key" - Direct key access

  • If no key provided, returns all translations for current language

Parameter Formatting: Supports both individual parameters and table parameters for string formatting.

Examples:

Automatic Features

Language Persistence

On the client-side, the library automatically:

  • Saves language preferences to KVP storage

  • Loads saved language on resource start

  • Syncs language changes across resources

FiveM Integration

The library integrates with FiveM's resource system:

  • Automatic resource detection

  • Cross-resource language synchronization

  • Event-driven language updates

Events

FRP:SetLanguage

Triggered when the language is changed.

Parameters:

  • language (string): New language code

Example:

Error Handling

The library includes robust error handling:

  • Returns "Error 502 : no translation available !" when language cache is unavailable

  • Falls back to the original key when translation is not found

  • Handles missing translation gracefully

Example:

Utility Functions

mysplit(inputstr, separator)

Internal utility function for splitting strings (used for parsing dot notation keys).

Parameters:

  • inputstr (string): String to split

  • separator (string, optional): Separator character (default: whitespace)

Returns: Table array of split strings

Usage Patterns

Setting Up Translations

Using Translations in Code

Client-Server Language Sync

Best Practices

  1. Consistent Key Naming: Use consistent prefixes and naming conventions

  2. Parameterized Strings: Use parameters for dynamic content

  3. Fallback Handling: Always check for translation availability

  4. Resource Organization: Keep translations organized by resource

Last updated