我們在我們的應用程序中使用Angular 2.0。在那個應用程序中,我們要給多語言支持。我們知道使用angular 1.0如何實現。但不知道如何在2.0中使用。Angular 2 - 多語言支持
回答
Angular2中的i18n仍在進行中,似乎尚未可用。
我得到了它 - 離子2 |國際化和本地化您的應用角2
http://www.gajotres.net/ionic-2-internationalize-and-localize-your-app-with-angular-2/
我可以推薦ngx-translate庫,它很容易集成和使用。
1.通過NPM在app.module.ts進口安裝
npm install @ngx-translate/core --save
2.添加TranslateModule
import {TranslateModule} from '@ngx-translate/core';
@NgModule({
declarations: [...],
imports : [TranslateModule.forRoot(), ...],
exports : [...],
providers : [...],
bootstrap : [AppComponent]
})
export class AppModule {}
3. app.components.ts
import {Component} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
@Component({
selector : 'app',
templateUrl: './app.component.html',
})
export class AppComponent {
constructor(private translate: TranslateService) {
translate.addLangs(['en', 'hy']);
translate.setDefaultLang('en');
translate.use('en');
}
changeLang(lang: string) {
this.translate.use(lang);
}
}
4. app.component.html
<nav>
<a [routerLink]="['/']">
{{ "home" | translate }}
</a>
|
<a [routerLink]="['/about']">
{{ "about" | translate }}
</a>
|
<a [routerLink]="['/contact']">
{{ "contact" | translate }}
</a>
</nav>
<div class="lang-bar">
<a (click)="changeLang('en')">EN</a>
<a (click)="changeLang('hy')">ՀՅ</a>
</div>
5.創建翻譯文件夾i18n的
en.json
{
"home" : "Home",
"about" : "About",
"contact" : "Contact"
}
hy.json
{
"home" : "Գլխավոր",
"about" : "Մեր մասին",
"contact" : "Հետադարձ կապ"
}
3. app.components.ts您不需要從'ng2-translate'導入TranslateService,而是從@ ngx-translate/core中導入它 – Jnana
您是否只需要將它添加到應用程序組件或使用它的每個組件中?因爲應用程序組件是所有東西的開始 - >所以在我的其他htmls中,我可以假設它已導入? – fangio
@fangio如果你想使用它,你必須導入'''TranslateService''',例如。你想改變當前的語言,檢測語言變化或獲得翻譯。如果你想使用像這樣的'''{{「contact」|你不需要在你的組件中從ngx-translate導入任何東西。 – styopdev
- 1. 多語言支持
- 2. 多語言支持
- 3. 在symfony 2中支持多種語言
- 4. JSF 2 - 多語言支持問題
- 5. SEO和多語言支持
- 6. Android多語言和少數語言支持語言不支持語言區域
- 7. iphone多語言支持
- 8. 多語言支持iPhone?
- 9. Spring MVC:多語言支持
- 10. Android多語言支持
- 11. 與多語言支持
- 12. HTML5支持多語言
- 13. QT多語言支持?
- 14. iphone多語言支持
- 15. PHP - 多語言支持
- 16. Mongodb的多語言支持
- 17. Mysql utf8_unicode_ci多語言支持
- 18. Django的多語言支持
- 19. 支持多種語言
- 20. Swift kitura多語言支持
- 21. IntelliJ Angular 2對*語法的HTML支持
- 22. Angular 2支持IE9?
- 23. ,它們支持多國語言</p> <p>多語言支持
- 24. Android語言支持
- 25. PyroCMS語言支持
- 26. EditText語言支持?
- 27. Yocto語言支持
- 28. 支持RTL語言
- 29. ILGeoNames語言支持
- 30. ios中的多語言支持
,它現在可用嗎?有沒有任何指導方針來做到這一點。 – peaceUser
它可用。檢查http://angular.io(我今天就在我的手機上) –
搭配在這方面怎麼樣照顧。 看起來像我們必須生成.xlf文件並進行渲染。另一種定義爲一種JSON並加載它。這是要做的嗎?如果需要翻譯動態數據,您會喜歡什麼? – peaceUser