Standalone API

Accessing locale message bundles

Once a default language bundle has been imported, any locale-specific messages are accessed by passing the message bundle to the i18n function.

For example:

  1. import { localizeBundle } from '@dojo/framework/i18n/i18n';
  2. import bundle from 'nls/main';
  3. localizeBundle(bundle, { locale: 'fr' }).then(({ messages }) => {
  4. console.log(messages.hello); // "Bonjour"
  5. console.log(messages.goodbye); // "Au revoir"
  6. });

If an unsupported locale is passed to i18n, then the default messages are returned. Further, any messages not provided by the locale-specific bundle will also fall back to their defaults. As such, the default bundle should contain all message keys used by any of the locale-specific bundles.

Determining the current locale

@dojo/framework/i18n/i18n exposes two related methods for determining the current locale:

  • getCurrentLocale, which represents the application’s current, top-level locale.
  • getComputedLocale, which will be either the user’s system locale if it is listed among the supported locales specified in the .dojorc, or the default locale specified in the .dojorc if the user’s system locale is not supported.