Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
839 views
in Technique[技术] by (71.8m points)

internationalization - Angular2 i18n at this point?

We decided to give it a spin and we started fresh project using Angular2. So far so good, but at this point we're facing an issue. At this point, what is the proper approach to i18n for Angular2? We've researched a little and found this:

However last commit is more than 5 months old... Doesn't look like active development.

Anyone tried using angular-translate or angular-gettext? Or maybe with Angular2 it's better to wrap something JS like i18next? Anyone could share their thoughts? Maybe you faced the same problem?

question from:https://stackoverflow.com/questions/34797512/angular2-i18n-at-this-point

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Plunk was updated to Angular 2 Final: https://plnkr.co/edit/4euRQQ. Things seem to work the same as in RC7.

New i18n section has been added to Angular 2 official docs. Basically, it explains in details what happens in the plunkr above.

XLIFF is the only format for translations, no json support. A translation source file (xliff, xlf) should be created using ng-xi18n tool:

package.json:

"scripts": {
  "i18n": "ng-xi18n", 
  ...
}

and

npm run i18n

See the Merge translation section for details about merging a translation into a component template. It's done using SystemJS Text plug-in.

Another example using Gulp http://www.savethecode.com/angular2-i18n-native-support/

Older staff: Update based on RC7 and links provided by Herman Fransen:

I've made a minimal Plunkr example: https://plnkr.co/edit/4W3LqZYAJWdHjb4Q5EbM

Comments to plunkr:

  • bootstrap should provide TRANSLATIONS, TRANSLATIONS_FORMAT, LOCALE_ID with values -> setup translations
  • translatable items in html-templates should use directive i18n
  • translations are stored in .xlf file. Ties between languages is done through Id, ties with html by a value of <source> tag in xlf
  • currently xlf files are not used directly; a .ts file is manually created to wrap the content of xlf in an exportable variable. I guess, this should be working automagically in final release (maybe even now).

This is the first officially documented approach I found. However, it's still barely usable. I see the following issues in the current implementation:

  • Language is set at bootstrap, unable to change it in run-time. This should be changed in Final.
  • Id of a translatable item in xlf is generated SHA. Current way to get this id is a bit messy: you create a new translatable item, use it, copy SHA id from error and paste into your i18n.lang.xlf file.

There is a big documentation pull request concerning i18n

Older staff: Release notes https://github.com/angular/angular/blob/master/CHANGELOG.md have a record

i18n: merge translations 7a8ef1e

A big chunk of i18n was introduced in Angular 2 RC5

Unfortunately, still no documentation available.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...