Angular 5 国际化 [英] Angular 5 internationalization

查看:42
本文介绍了Angular 5 国际化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新的 Angular5 构建一个应用程序,我需要的是让用户能够切换语言.我从来没有在 Angular2+ 中实现过这个(实际上我使用的是 Angular5).

I'm building an application using the latest Angular5 and what I need is for a user to be able to switch languages. I've never had to implement this in an Angular2+ (actually I'm using Angular5).

我需要在两个地方设置翻译:

I need to set translations in two places:

  • 组件的模板 html - 将标签更改为指定语言
  • 在 component.ts 文件中的代码 - 我可能需要翻译一些在代码中的特定条件下动态构建的字符串

我正在查看 ngx-translation 并且它看起来可以做我需要的一切,就像它一样允许您在不重新构建代码的情况下更改语言,请参阅此处.但是我读到 它可能会被弃用,因为主要开发人员转向Angular 团队开发他们的 i18n 代码.

I was looking at ngx-translation and it looks to do everything I need, as in it allows you to change language without rebuilding your code, see here. However I read it was probably going to be deprecated due to the main developer moving to the angular team to develop their i18n code.

我也了解当前的 i18n 不支持我现在需要的一切,请参阅 此处.

I also understand that the current i18n doesn't support everything I need right now, see here.

我的问题 - 最新版本的 Angular 中的翻译状态如何?如果确实,Angular 本身还没有得到完全支持(用于在不重新编译的情况下更改语言),人们是否会推荐其他库?ngx-translate 对未来有好处吗?

My question - what is the state of play for translations in the latest version of Angular? Are there other libraries people would recommend instead, if indeed, Angular itself hasn't got full support as of yet (for changing language without recompiling)? Is ngx-translate good for the future?

非常感谢这方面的任何指导!

Any guidance in this area is greatly appreciated!

推荐答案

在花时间研究这个之后,我想我会发布我在 ngx-translateAngular-i18n:

After spending time looking into this, I thought I'd post the main differences I found between ngx-translate and Angular-i18n:

  • Angular 一次只能使用一种语言,您必须完全重新加载应用程序才能更改语言.JIT 支持仅意味着它与 JIT 一起工作,但您仍然必须在引导程序时提供翻译,因为它将在编译期间替换模板中的文本,而此库使用绑定,这意味着您可以随时更改翻译.缺点是绑定会占用内存,因此 Angular 方式的性能更高.但是如果你对你的组件使用 OnPush,你可能永远不会注意到区别
  • Angular 目前仅支持在您的模板中使用 i18n,我正在开发允许您在代码中使用它的功能,但它仍在进行中.这个库在代码和模板中都有效
  • Angular 支持 XLIFF 或 XMB(两者都是 XML 格式),而这个库默认支持 JSON,但您可以编写自己的加载器来支持您想要的任何格式(例如,有一个用于 PO 文件的加载器).就个人而言,Json 文件比这些其他格式更易于阅读,但这并不是一个很大的缺点.
  • Angular 支持 ICU 表达式(复数和选择),但该库不支持
  • Angular 支持 html 占位符,包括 Angular 代码,而这个库只支持常规的 html(因为它是在运行时执行的,而不是在编译期间执行,并且 Angular 中没有 $compile 像 AngularJS 中那样)
  • 这个库的 API 更加完整,因为它是在运行时执行的,它可以提供更多 Angular 没有的东西(可观察的、事件等)(但实际上并不需要,因为你不能改变翻译)ngx-translate 的创建者是这样说的:

Ocombe(ngx 的开发者):@josersleal 这正是他们所做的,angular 团队聘请我为每个人改进 i18n 但是没有工作 3 后,将我的 lib 直接集成到核心中的方法核心团队的几个月我可以告诉你,Angular i18n 远不止这些比我的 lib 复杂和精致.它处理很多更复杂的东西,它没有我的所有错误和缺点lib 有.我知道核心不支持令人沮丧与图书馆的发展速度一样快,但有理由那个,第一个是你不能实现一些东西每当您发现忘记包含用例时,就更改它.一切都必须经过周密的计划和思考.还是你会拥有这个库可以在核心中做的大部分事情未来,但不幸的是,我们可能需要一年的时间才能到达那里.好消息是它会比我的天真好得多实施.

Ocombe (developer of ngx): @josersleal that's exactly what they did, the angular team hired me to improve i18n for everyone But there is no way to integrate my lib directly into the core, after working for 3 months for the core team I can tell you that Angular i18n is much more complex and elaborate than my lib. It handles a lot of more complex stuff, and it does it without all the bugs and shortcomings that my lib has. I understand that it's frustrating that the core doesn't evolve as fast as what a library can do, but there are reasons for that, and the first one is that you cannot implement something and change it whenever you see that you forgot to include a use case. Everything has to be thoroughly planned and thought. Still, you will have most of the things that this lib can do in the core in the future, but it might take a year before we get there unfortunately. The good news is that it's going to be much better than my naive implementation.

这是一篇讨论 ngx-translate 和 Angular 的 i18n 之间主要区别的好文章:https://github.com/ngx-translate/core/issues/495

This is a good article to discuss the main differences between ngx-translate and Angular’s i18n: https://github.com/ngx-translate/core/issues/495

i18n 的更改将在 angular 的第 6 版中进行.今天,我们目前使用的是第 5 版:

The changes for i18n are due in version 6 of angular. Today, we are currently on version 5:

  • 不会是 5.0,应该是 6.0 之前(所以在 2018 年 3 月之前).不幸的是,我没有更准确的日期

  • It won't be for 5.0, it should be before 6.0 (so before march 2018). Unfortunately I don't have a more precise date

ngx-translate 的开发者(现在是 angular-i18n 的主要贡献者)在 12 天前发布了这篇文章:https://github.com/angular/angular/issues/20193

The developer of ngx-translate (and now a main contributor to angular-i18n) has posted this 12 days ago: https://github.com/angular/angular/issues/20193

这是 i18n 的设计文档(现有技术部分很有趣):https://docs.google.com/document/d/1LH7lJ1GjRgpGUjzNb6Eg4xrPooRdi80QOTYYh8z_JyY/edit#" rel="noreferrer">https://docs.google.com/document/d/1LH7lJ1GjRgpYYG4Nb6Eg4xrPooRdi80QOTYYh8z_JyY/edit#"一个>

Here’s the design document for i18n (the Prior Art section is interesting): https://docs.google.com/document/d/1LH7lJ1GjRgpGUjzNb6Eg4xrPooRdi80QOTYYh8z_JyY/edit#

一些想法......

  • Angular-i18n 的性能更高,因为您使用所需的语言编译应用程序(而不是在运行时进行翻译).也可能是一个缺点,因为您可能需要使用不同语言构建多个应用程序.
  • 如果我们使用 SEO,由于 url 浏览,angular-i18n 将是前进的方向.就我而言,我根本不需要这个.
  • 如果我们需要复数切换等.同样,我不需要这个 - 我只需要在模板和代码中进行相当直接的运行时语言切换.
  • Angular-i18n 至少要到 2018 年 3 月才会发布.对我来说,我不能等到那时,因为我现在需要构建我的应用程序.
  • ngx-translate 不会像 angular-i18n 那样提供全面的功能,但我只需要简单的运行时翻译,所以认为它可以满足我们的需求.
  • ngx-translate 是开源的,在它不再被开发的那一天,如果有严重的问题,我想我可以自己解决(希望到时候,可能出现的任何问题都会得到解决).

我还将查看 Angular-l10n 库,因为它看起来非常好:

I am also going to have a look at the Angular-l10n library as it looks very good:

这篇关于Angular 5 国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆