Internationalization Service

Main class to be used by user isI18NService. I18N is the standard acronym for the word Internationalization. It's implemented as a Decorator for other classes that implement particular logic.

I18NServiceshould be included in the project root class, and then it's accessible as a singleton everywhere in the application.

Localization

By default, the service takes browser language and sets default locale accordingly. User can always change locale by usingsetLocale(locale: string)method. This method takes standard locales as a string, likeen-US, fr-FR, fr-CAetc. First part is language, second is country.

  /**
     * <p> Sets new locale and returns Promise when new dictionary is uploaded to memory.</p>
     * <p> Locale format examples: en-US, fr-FR, fr-CA, de-DE etc.</p>
     */
    setLocale(locale: string): Promise<any>

Dates Formatting

Use this method to format date with current locale and pass component name if you want to use date format from component dictionary:

  /**
     * <p> Format date according to the current locale.</p>
     * <p> Date format is taken from dictionary.</p>
     */
    formatDate(value: any, componentName?: string): string

Service wrapsmoment.jsto provide date formatting and, therefore, can take its acronyms as date formats:

By convention, date format should be under "dateFormat" path, for example:

Currency Formatting

Service uses standard JavaScript possibilities to format currency. Method to format it is:

As for other methods, you can pass component name to take currency format from component dictionary. By convention, currency format should be under "currency" path, for example:

Messages Formatting - Dictionary

Dictionary is an interface to translate messages. These messages are placed in the JSON files named by language, ex: en.json for English language and fr.json for French language. There is a default dictionary placed underassets/i18nfolder. User can create its own default dictionary and set its URL:

Every component can use its own dictionary by setting its URL in:

User can get translated messages by using this method:

When using the particular component dictionary, you should pass component name as a parameter.

Example

The complete example code can be found under Examples/Internationalization of the Cedrus/CodeFusion project.

Important note is that when user changes locale, another dictionary should be put in memory. Therefore, setLocale() method, as well as setDictionaryURL() and setComponentDictionaryURL() methods returns promises to let system wait until new dictionary is downloaded.

Internationalize Button Component Label

This is HTML used to render CfButton component.

This is the component model and simple label internationalization using the default dictionary:

If user changes locale, for example, by choosing country in drop down list, then in the listener, the messages should be reset from new dictionary:

Internationalize Business Component

Lets internationalize the part of the CfWeatherComponent. First, we have to import i18n service and a Subscription class:

Then, we should create a method where we put all our internationalization code. Note, that if we want to use a separate dictionary for this component, we can do it with setComponentDictionaryURL() method:

Then, we should call internationalize() method and, if we want the component to change its messages when user changes locale, we should subscribe on the locale changes:

Also, do not forget to unsubscribe when component is destroyed to avoid memory leaks:

This is CfWeatherComponent component HTML to be rendered:

This is the code to use:

That's it, you should do nothing more, when user changes locale, the component will change its messages accordingly.

Angular 2 Internationalization Tools

User can always use standard Angular 2 tools, like:

But they will not react on the fact that user changes the localization dynamically, for example in drop-down list and passes it to i18n service. They will react only on browser settings changes.

But, all methods provided by i18n service will react on dynamic locale changes.

Last updated