Translation / i18n
- Localisation Helper Class
- Methods
- Example Usage
- locales.js
- Example Usage in locales.js
- Routing for JSON localisation
- Caching
Localisation Helper Class
The I18n
class in Sukarix provides methods to retrieve localized strings from different translation tables. There are
four types of translation tables:
- Labels (
i18n.label
) - Messages (
i18n.message
) - Errors (
i18n.error
) - Lists (
i18n.list
)
Methods
lbl
Fetches a localized label.
public function lbl($key): string
msg
Fetches a localized message.
public function msg($key): string
err
Fetches a localized error message.
public function err($key): string
lst
Fetches a localized list.
public function lst($key): array
Example Usage
$i18n = Injector::instance()->get('i18n');
echo $i18n->lbl('welcome'); // Fetches the 'welcome' label
echo $i18n->msg('greeting'); // Fetches the 'greeting' message
echo $i18n->err('not_found'); // Fetches the 'not_found' error message
print_r($i18n->lst('countries')); // Fetches the 'countries' list
locales.js
The locales.js
file handles loading and switching of localisation data in JSON format.
Functions
- loadLocale: Loads the locale data from a JSON file.
- init: Initializes the locale settings and language menu.
- initLanguageMenu: Sets up the language selection menu.
- setLocale: Sets the current locale.
- switchLocale: Switches to the selected locale and updates the displayed strings.
- translaterStrings: Updates all strings in the document with localized content.
- get: Retrieves a localized string based on type, module, and key.
- err: Retrieves a localized error message.
- lbl: Retrieves a localized label.
- loc: Retrieves a localized string.
- msg: Retrieves a localized message.
- lst: Retrieves a localized list item.
Example Usage in locales.js
Locale.init(); // Initializes the locale settings
Locale.setLocale('en-GB'); // Sets the current locale to 'en-GB'
Locale.switchLocale('fr-FR'); // Switches to 'fr-FR' and updates the displayed strings
Routing for JSON localisation
The routing for generating JSON localisation files is defined as follows:
GET @locale: /locale/json/@locale.json = Sukarix\Actions\Core\GetLocale->execute
Caching
JSON locale data is cached whenever the translation file is updated to improve performance and reduce load times.