It is just quick startup guide for programmers who already know the concept from below link:
https://angular.io/guide/i18n
Setup Angular project
import { LOCALE_ID} from '\@angular/core';
providers: [ { provide: LOCALE_ID, useValue: 'fr' } ],
import { registerLocaleData } from '\@angular/common';
import localeFr from '\@angular/common/locales/fr';
registerLocaleData(localeFr);
ng xi18n –outputPath src/locale
By default, the tool generates a translation file named messages.xlf
You can specify the translation format explicitly with the --i18nFormat flag as illustrated in these example commands:
ng xi18n --i18nFormat=xlf ng xi18n --i18nFormat=xlf2 ng xi18n --i18nFormat=xmb
Make copies of message.xlf file like message.fr.xlf and and node corresponding to source nodes manually for testing. You can also use XLIFF file editor.
Add below code in main.ts
//use the require method provided by webpack
declare const require;
//we use the webpack raw-loader to return the content as a string
const translations = require(`raw-loader!./locale/messages.fr.xlf`);
platformBrowserDynamic().bootstrapModule(AppModule, {
providers: [
{ provide: TRANSLATIONS, useValue: translations },
{ provide: TRANSLATIONS_FORMAT, useValue: 'xlf' }
]
}) .catch(err => console.log(err));
follow step 4 for JIT compilation
Run project with below command.
ng serve --aot --i18nFile=src/locale/messages.fr.xlf --i18nFormat=xlf --locale=fr
Change the file and format as per your case
Note: you can keep the lengthy commands in packages json script to remember.
The i18n template translation process has four phases:
Mark static text messages in your component templates for translation.
An Angular i18n tool extracts the marked text into an industry standard translation source file.
A translator edits that file, translating the extracted text into the target language, and returns the file to you.
The Angular compiler imports the completed translation files, replaces the original messages with translated text, and generates a new version of the app in the target language.
You need to build and deploy a separate version of the app for each supported language.