Introduction

Dojo’s i18n package solves a variety of common requirements and challenges around web application internationalization.

While a large subset of its functionality can be used as a standalone module, it is intended for use in Dojo applications to help render localized widgets, including advanced message, date and number formatting.

FeatureDescription
Per-widget localizationEach widget instance can have its own locale specified, allowing data from multiple locales to be displayed within a single application. If not specified, widgets fall back to the current root locale.
Fine-grained message bundlingBundles can be decomposed and scoped locally to individual widgets, and can be lazily-loaded only if a given locale is in use. This allows message bundles to benefit from the same layer separation & bundled delivery as all other resources within an application.
Locale-specific message, date, and number formattingUses industry-standard Unicode CLDR formatting rules. The required CLDR data are automatically injected into applications at build time.
Reactive locale change responseSimilar to other reactive state changes within a Dojo application, messages can be automatically reloaded and affected widgets re-rendered when changing locales.If using i18n as a standalone module, locale change events can be acted on via listener callbacks.
Fallback locale detectionEnsures a default locale is available if a root override has not been explicitly set.The system locale (or the process/host locale when running server-side) is matched against the list of locales defined in the .dojorc. If the system locale is supported, then it is set as the default. Otherwise, the default locale defined in the .dojorc is used.

Basic usage

Internationalizing a widget

  • Starting off with a single default language (English).

.dojorc

  1. {
  2. "build-app": {
  3. "locale": "en"
  4. }
  5. }

src/widgets/MyI18nWidget.tsx

Function-based variant:

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import i18n from '@dojo/framework/core/middleware/i18n';
  3. import myWidgetMessageBundle from '../nls/en/MyI18nWidget.ts';
  4. const factory = create({ i18n });
  5. export default factory(function MyI18nWidget({ middleware: { i18n } }) {
  6. const { messages } = i18n.localize(myWidgetMessageBundle);
  7. return <div title={messages.title}>{messages.content}</div>;
  8. });

Class-based variant:

  1. import { WidgetBase } from '@dojo/framework/core/WidgetBase';
  2. import { tsx } from '@dojo/framework/core/vdom';
  3. import I18nMixin from '@dojo/framework/core/mixins/I18n';
  4. import myWidgetMessageBundle from '../nls/en/MyI18nWidget.ts';
  5. export default class MyI18nWidget extends I18nMixin(WidgetBase) {
  6. protected render() {
  7. const { messages } = this.localizeBundle(myWidgetMessageBundle);
  8. return <div title={messages.title}>{messages.content}</div>;
  9. }
  10. }

src/nls/en/MyI18nWidget.ts

  1. export default {
  2. messages: {
  3. title: 'Hello',
  4. content: 'This is an internationalized widget'
  5. }
  6. };

Adding a widget language localization bundle

  • Supporting two locales - English as the default, together with a French translation that is activated for any users that have fr set as their primary language.

.dojorc

  1. {
  2. "build-app": {
  3. "locale": "en",
  4. "supportedLocales": [ "fr" ]
  5. }
  6. }

src/nls/en/MyI18nWidget.ts

  1. export default {
  2. locales: {
  3. fr: () => import('../fr/MyI18nWidget')
  4. },
  5. messages: {
  6. title: 'Hello',
  7. content: 'This is an internationalized widget'
  8. }
  9. };

src/nls/fr/MyI18nWidget.ts

  1. export default {
  2. title: 'Bonjour',
  3. content: 'Ceci est un widget internationalisé'
  4. };

Specifying a root locale within an application

Using function-based widgets and i18n middleware exclusively within an application means there is no requirement to add bootstrapping code to the application’s main.ts/main.tsx entry point. The default application locale can instead be set in a top-level App widget, using the i18n middleware from @dojo/framework/core/middleware/i18n. A default locale can be set when a locale has not already been defined.

src/App.tsx

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. const factory = create({ i18n });
  3. export default factory(function App({ middleware: { i18n } }) {
  4. if (!i18n.get()) {
  5. i18n.set({ locale: 'en-us', rtl: false });
  6. }
  7. return <div>{/* the application widgets */}</div>;
  8. });

However, if the application uses any class-based widgets, such as those from the @dojo/widgets suite, the default locale details will need to be defined in the application registry. This can be done using the registryI18nInjector utility function, available from @dojo/framework/core/mixins/I18n.

src/main.tsx

  1. import renderer, { tsx } from '@dojo/framework/core/vdom';
  2. import Registry from '@dojo/framework/core/Registry';
  3. import { registerI18nInjector } from '@dojo/framework/core/mixins/I18n';
  4. import App from './App';
  5. const registry = new Registry();
  6. registerI18nInjector({ locale: 'en-us', rtl: false }, registry);
  7. const r = renderer(() => <App />);
  8. r.mount({ registry });

Changing the locale within an application

  • Using the i18n middleware to allow users to to choose between supported locales, and enact a locale change via the middleware’s .set API.

Reminder: When using both class-based and function-based widgets, this middleware should be used together with registeri18nInjector to reactively propagate locale changes to all i18n-aware widgets.

src/widgets/LocaleChanger.tsx

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import i18n from '@dojo/framework/core/middleware/i18n';
  3. const factory = create({ i18n });
  4. export default factory(function LocaleChanger({ middleware: { i18n } }) {
  5. return (
  6. <div>
  7. <button
  8. onclick={() => {
  9. i18n.set({ locale: 'en' });
  10. }}
  11. >
  12. English
  13. </button>
  14. <button
  15. onclick={() => {
  16. i18n.set({ locale: 'fr' });
  17. }}
  18. >
  19. French
  20. </button>
  21. </div>
  22. );
  23. });