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 i18n, { Messages } from '@dojo/framework/i18n/i18n';
  2. import bundle from 'nls/main';
  3. i18n(bundle, 'fr').then(function(messages: 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.

Alternatively, locale messages can be manually loaded by passing them to setLocaleMessages. This is useful for pre-caching locale-specific messages so that an additional HTTP request is not sent to load them. Locale-specific messages are merged with the default messages, so partial message bundles are acceptable:

  1. import i18n, { setLocaleMessages } from '@dojo/framework/i18n/i18n';
  2. import bundle from 'nls/main';
  3. const partialMessages = { hello: 'Ahoj' };
  4. setLocaleMessages(bundle, partialMessages, 'cz');
  5. i18n(bundle, 'cz').then((messages) => {
  6. console.log(messages.hello); // "Ahoj"
  7. console.log(messages.goodbye); // "Goodbye" (defaults are used when not overridden)
  8. });

Once locale dictionaries for a bundle have been loaded, they are cached and can be accessed synchronously via getCachedMessages:

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

getCachedMessages will look up the bundle’s supported locales to determine whether the default messages should be returned. Locales are also normalized to their most specific messages. For example, if the ‘fr’ locale is supported, but ‘fr-CA’ is not, getCachedMessages will return the messages for the ‘fr’ locale:

  1. import { getCachedMessages } from '@dojo/framework/i18n/i18n';
  2. import bundle from 'nls/main';
  3. const frenchMessages = getCachedMessages(bundle, 'fr-CA');
  4. console.log(frenchMessages.hello); // "Bonjour"
  5. console.log(frenchMessages.goodbye); // "Au revoir"
  6. const madeUpLocaleMessages = getCachedMessages(bundle, 'made-up-locale');
  7. console.log(madeUpLocaleMessages.hello); // "Hello"
  8. console.log(madeUpLocaleMessages.goodbye); // "Goodbye"

If need be, bundle caches can be cleared with invalidate. If called with a bundle, only the messages for that particular bundle are removed from the cache. Otherwise, all messages are cleared:

  1. import i18n, { getCachedMessages, invalidate } from '@dojo/framework/i18n/main';
  2. import bundle from 'nls/main';
  3. i18n(bundle, 'ar').then(() => {
  4. invalidate(bundle);
  5. console.log(getCachedMessages(bundle, 'ar')); // undefined
  6. });

Determining the current locale

The current locale can be accessed via the read-only property i18n.locale, which will always be either the locale set via switchLocale (see below) or the systemLocale.

The systemLocale is also read-only, and its value is determined by the current execution environment in the following manner:

EnvironmentLocale
BrowserUser’s default language setting
Node.jsThe Node.js process’s LANG environment variable.
Fallbacken

Changing the root locale and observing locale changes

The switchLocale method changes the root locale and notifies all consumers registered with observeLocale, which accepts a function that receives the new locale string as its sole argument. For example, suppose the system locale is en-GB:

  1. import i18n, { observeLocale, switchLocale, systemLocale } from '@dojo/framework/i18n/i18n';
  2. import bundle from 'nls/bundle';
  3. // Register an event listener
  4. observeLocale((locale: string) => {
  5. // handle locale change...
  6. });
  7. // Change the locale to German. The registered observer's callback will be called
  8. // with the new locale.
  9. switchLocale('de');
  10. // The locale is again switched to German, but since the current root locale is
  11. // already German, registered observers will not be notified.
  12. switchLocale('de');
  13. console.log(i18n.locale); // 'de'
  14. console.log(systemLocale); // 'en-GB' (the system locale does not change with the root locale)