Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / XHTML

Angular2 & WebApi (SPA) for Enterprise App - Part 4 - Multi-Languages (i18n)

3.93/5 (11 votes)
28 May 2017CPOL3 min read 47.4K  
In this article, we will see how to use multi-languages(i18n) in angular2

Other Articles in the Series

  1. Overview
  2. Add new Permission
  3. Project structure
  4. Multi-Languages (i18n)
  5. DI & IoC - Why and Why not?
  6. RESTful & WebApi
  7. Manage Application Lifecycle
  8. Build & Deploy Application
  9. New version of TinyERP with Angular 2 (typescript)
  10. CQRS: Avoiding performance issues in enterprise app (basic)
  11. Multiple data-stores: Scale your repository (Part 1)
  12. Multiple data-stores: Scale your repository (Part 2)
  13. Basic authentication (user name/ password) with OWIN

Introduction

I have looked around the internet. Some others have already contributed their solution for multi-languages in Angular2.

Look at my code. I have another solution for this case, it looks easier to use on both ts code or from html file.

How to Get the Code

Please check out the code at https://github.com/techcoaching/TinyERP.

How to Use the Code

Look at "<root>/app/modules/security/role/addRole.html", we see the following HTML:

HTML
<page>
    <page-header>
        {{i18n.security.addOrUpdateRole.title}}
    </page-header>
    <page-content>
        <form-default>
            <form-text-input [placeHolderText]=i18n.security.addOrUpdateRole.inputName 
            [labelText]=i18n.security.addOrUpdateRole.name
                [validation]="['security.addOrUpdateRole.validation.nameIsRequire',
                'security.addOrUpdateRole.validation.keyAlreadyExisted']"
                [(model)]=model.name>
            </form-text-input>
            <form-textarea [placeHolderText]=i18n.security.addOrUpdateRole.inputDesc 
            [labelText]=i18n.security.addOrUpdateRole.desc [(model)]=model.description>
            </form-textarea>

            <form-permission-select [(values)]=model.permissions 
            [placeHolderText]=i18n.security.addOrUpdateRole.inputPermissions 
            [labelText]=i18n.security.addOrUpdateRole.permission
                [(model)]=model.permissions>
            </form-permission-select>

            <form-footer>
                <button id="save" 
                (click)="onSaveClicked($event)" type="button" 
                 class="btn btn-primary">{{i18n.common.form.save}}</button>
                <button id="cancel" 
                (click)="onCancelClicked($event)" type="button" 
                 class="btn btn-default">{{i18n.common.form.cancel}}</button>
            </form-footer>
        </form-default>
    </page-content>
</page>

In this HTML, we use some texts from locale file, such as: i18n.security.addOrUpdateRole.inputName, i18n.security.addOrUpdateRole.name, ...

There is a convention for those texts, they are in format: i18n.<moduleName>.<name of page>.<name of field>. For example, "i18n.security.addOrUpdateRole.name", we understand that this is the text for name field on addOrUpdateRole page in security module.

For a better understanding about moduleName, see Angular2 & WebApi(SPA) for Enterprise App - Part 3 - Project Structure.

The value for those was located in "<root>/app/resosurces/locales/<moduleName>.<language>.json", for example security.en.json will contain all text that was used in security module in English language.

For more information, how did I implement this, please have a look at "<root>/app/models/ui/baseComponent.ts" (for i18nHelper and i18n properties).

Other Articles in the Series

  1. Single Page Application (SPA) for Enterprise App (Angular2 & WebApi)
  2. Single Page Application (SPA) for Enterprise App (Angular2 & WebApi) - Part 2
  3. Angular2 & WebApi (SPA) for Enterprise App - Part 3 - Project structure

Introduction

I have looked around the internet. Some others have already contributed their solution for multi-languages in Angular2.

Look at my code. I have another solution for this case, it looks easier to use on both ts code or from html file.

How to Get the Code

Please check out the code at https://github.com/techcoaching/TinyERP.

How to Use the Code

Look at "<root>/app/modules/security/role/addRole.html", we see the following HTML:

HTML
<page>
    <page-header>
        {{i18n.security.addOrUpdateRole.title}}
    </page-header>
    <page-content>
        <form-default>
            <form-text-input [placeHolderText]=i18n.security.addOrUpdateRole.inputName 
            [labelText]=i18n.security.addOrUpdateRole.name
                [validation]="['security.addOrUpdateRole.validation.nameIsRequire',
                'security.addOrUpdateRole.validation.keyAlreadyExisted']"
                [(model)]=model.name>
            </form-text-input>
            <form-textarea [placeHolderText]=i18n.security.addOrUpdateRole.inputDesc 
            [labelText]=i18n.security.addOrUpdateRole.desc [(model)]=model.description>
            </form-textarea>

            <form-permission-select [(values)]=model.permissions 
            [placeHolderText]=i18n.security.addOrUpdateRole.inputPermissions 
            [labelText]=i18n.security.addOrUpdateRole.permission
                [(model)]=model.permissions>
            </form-permission-select>

            <form-footer>
                <button id="save" 
                (click)="onSaveClicked($event)" type="button" 
                 class="btn btn-primary">{{i18n.common.form.save}}</button>
                <button id="cancel" 
                (click)="onCancelClicked($event)" type="button" 
                 class="btn btn-default">{{i18n.common.form.cancel}}</button>
            </form-footer>
        </form-default>
    </page-content>
</page>

In this HTML, we use some texts from locale file, such as: i18n.security.addOrUpdateRole.inputName, i18n.security.addOrUpdateRole.name, ...

There is a convention for those texts, they are in format: i18n.<moduleName>.<name of page>.<name of field>. For example, "i18n.security.addOrUpdateRole.name", we understand that this is the text for name field on addOrUpdateRole page in security module.

For a better understanding about moduleName, see Angular2 & WebApi(SPA) for Enterprise App - Part 3 - Project Structure.

The value for those was located in "<root>/app/resosurces/locales/<moduleName>.<language>.json", for example security.en.json will contain all text that was used in security module in English language.

For more information, how did I implement this, please have a look at "<root>/app/models/ui/baseComponent.ts" (for i18nHelper and i18n properties).

For more information about:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)