Change the application language
Once you translate your application, you can change the default language used by the application. Individual users can also set their prefered language.
Language resolution order
When a user accesses the application, the locale used for that user is:
- The user preferred locale in UiSdlUserPreference, if it is set and if that locale is declared.
- The first locale declared in UiSdlI18nConfig#i18n
- The
enlocale if the application does not declare any locales.
Change the locale for a single user
To change the locale for a single user, make sure that locale is declared in UiSdlI18nConfig#i18n, and use:
let locale = `es`;
UiSdlUserPreference.inst().setConfigValue('preferredLocale', locale)If you're not sure what locales are available in an application, you can query them through:
UiSdlConfig.inst().i18n.localesChange the default locale of the application
The prefered way to change the default locale of the application, is by declaring the locales in your application package. This ensures your application will have the same defaults as you move the application accross different environments.
To change the default locale for an individual application deployment, from console, run:
// Get a declared Locale to be the new default
let locale = Locale.forId('en')
// Get declared locales
let config = UiSdlConfig.inst()
// See if the locale is already in the array and remove it if it is.
let oldLocalePosition = config.i18n.locales.indexOf(config.i18n.locales.find(loc => loc?.id === locale?.id));
let removeOld = config.i18n.locales;
if (oldLocalePosition >= 0) {
removeOld = removeOld.removeAt(oldLocalePosition);
}
// Prepend the locale, so it becomes the default when user does not have a preference
let newLocalePreference = removeOld.withFirst(locale);
// Update the application-level configuration
config.setConfigValue('i18n.locales', newLocalePreference)