I want to build a web-application with angular2 and bundle this with webpack. What is the best way for providing multiple languages:
i18n-plugin: https://github.com/webpack/i18n-webpack-plugin
or
ng2-translate: https://github.com/ocombe/ng2-translate
Angular 2 Final release has i18n native support https://angular.io/docs/ts/latest/cookbook/i18n.html
Check another answer https://stackoverflow.com/a/39004058/1267942 with example and some details about usage.
ng2-translate is too verbose comparing to the native implementation. Also the author of ng2-translate is a big contributor to angular 2 i18n documentation
I got it working with webpack using the cookbook. The xliff file has to be converted to ts like so:
Then in the main.ts it has to be added
and inserted to the bootstrap step:
A slightly modified version of @M.Nour Berro's answer.
I made this change as synchronous xhr's are deprecated and possibly support might be removed later.
in case someone still wondering how to use angular 2 localization with webpack loader, i have modified the provider angular 2 cookbook provided;
First create i18n.provider.ts
notice the changes are :
Second in the main.ts files change the bootstrapping the same as mentioned in angular cookbook
If you are using
angular-cli
you can follow these steps:Extract messages with
ng xi18n
command with location for translation file given:You will get
src/i18n/messages.xlf
file. Copy this file and translate messages to languages you need:Serve/build your app with
ng serve / ng build
command (change locale accordingly):