简体   繁体   English

Angular 6 - i18n vs. ngx-translate

[英]Angular 6 - i18n vs. ngx-translate

I am working on a large project using angular 6. This project needs significant i18n integration. 我正在使用角度6进行大型项目。该项目需要大量的i18n集成。 I am trying to make the right decision as to how to proceed. 我正在努力做出正确的决定如何继续。

The way I see it is that I can choose between: 我看到它的方式是我可以选择:

  1. ngx-translate ( https://github.com/ngx-translate/core ) ngx-translate( https://github.com/ngx-translate/core
  2. Angular i18n ( https://angular.io/guide/i18n ) Angular i18n( https://angular.io/guide/i18n

I am leaning toward choosing option 2; 我倾向于选择选项2; Angular's i18n. Angular的i18n。 This is because it is Angular's own built in package and that just sits better with me. 这是因为它是Angular自己的内置包,而且对我来说更好。 Apparently it is also better for SEO and marginally better for performance without any work around. 显然它对SEO也更好,并且在没有任何解决方法的情况下性能稍微好一些。 Also, now it is released I think ngx-translate might be deprecated. 此外,现在它发布我认为ngx-translate可能已被弃用。 Lot's of info here: Angular 5 internationalization . 很多信息在这里: Angular 5国际化

However here are my reservations: 不过这是我的保留:

  • Apparently Angular's version is quite new / still catching up ( https://github.com/ngx-translate/core/issues/495 ). 显然Angular的版本很新/仍在追赶( https://github.com/ngx-translate/core/issues/495 )。 Is it too new to take on? 是不是太新了?
  • Apparently I have to have a separate website build for each language ( https://angular.io/guide/i18n#template-translations )??? 显然我必须为每种语言建立一个单独的网站( https://angular.io/guide/i18n#template-translations )??? I really don't think that would be a good thing. 我真的不认为那会是一件好事。 Is that right? 那正确吗? or is it a case that template files are dynamically inserted each time? 或者是每次动态插入模板文件的情况? I know that with ngx-translate the translated words are just stored in .json files - I like the neatness of that. 我知道用ngx-translate翻译的单词只存储在.json文件中 - 我喜欢它的整洁。 I don't think we want a bunch of different website builds. 我认为我们不想要一堆不同的网站构建。

Is there a way to do live language switching with angular i18n? 有没有办法用角度i18n进行实时语言切换? ie switching language on the page without reloading / refetching data from the server? 即在不重新加载/从服务器获取数据的情况下切换页面上的语言? Is that what they call JIT? 这就是他们所说的JIT吗?

Has Anyone tried https://github.com/robisim74/angular-l10n ? 有没有人试过https://github.com/robisim74/angular-l10n looks very good also. 看起来也很好。

Many thanks. 非常感谢。 Best regards. 最好的祝福。

Is it too new to take on? 是不是太新了?

This is opinion-based, and thus off-topic 这是基于意见的,因此是偏离主题的

Apparently I have to have a separate website 显然我必须有一个单独的网站

You need a separate application (ie index.html + bundles). 您需要一个单独的应用程序(即index.html + bundle)。 But you could serve all those applications from a single URL, deciding which one to serve on the server. 但是,您可以从单个URL提供所有这些应用程序,从而决定在服务器上提供哪个应用程序。 That will (hopefully) change with Angular 7, when the new dynamic i18n, built on top of Ivy, is available. 当希望建立在Ivy之上的新动态i18n可用时,它将(希望)改变Angular 7。

is it a case that template files are dynamically inserted each time? 是每次动态插入模板文件的情况吗?

Not sure what you mean. 不明白你的意思。 The translations of the locale you build for are stored in the generated template classes at compile time, by the Angular AOT compiler. 您构建的语言环境的翻译在编译时由Angular AOT编译器存储在生成的模板类中。

Is there a way to do live language switching with angular i18n? 有没有办法用角度i18n进行实时语言切换?

No, and that won't be possible with the next version of i18n either. 不,对于下一版本的i18n,这是不可能的。 The same, unique application will be able to run in several languages, but the language will still have to be chosen at startup, and you'll have to restart the app to choose another language. 同样的,独特的应用程序将能够以多种语言运行,但仍然必须在启动时选择语言,并且您必须重新启动应用程序才能选择其他语言。

Is there a way to do live language switching with angular i18n? 有没有办法用角度i18n进行实时语言切换?

No. Not efficiently at least. 不,至少没有效率。

Is that what they call JIT? 这就是他们所说的JIT吗?

No. The JIT compiler is what compiles your HTML templates into JavaScript at startup, in the browser. 不.JIT编译器是在启动时在浏览器中将HTML模板编译成JavaScript的。 In production, you use the AOT compiler (which is also used to integrate the translations into the generated JS classes), which does a similar compilation of the templates, but at build time rather than runtime. 在生产中,您使用AOT编译器(也用于将翻译集成到生成的JS类中),它对模板进行类似的编译,但是在构建时而不是运行时。

The discussion is still on, and here you can find some answers and opinions, even directly from the Angular developers: https://github.com/ngx-translate/core/issues/495 讨论仍在进行,在这里您可以找到一些答案和意见,甚至直接来自Angular开发人员: https//github.com/ngx-translate/core/issues/495

Personally, I would structure the app with the official i18n and eventually add some dedicated translation in the code with the ngx-translate library. 就个人而言,我会使用官方的i18n构建应用程序,并最终在代码中使用ngx-translate库添加一些专用翻译。

Is there a way to do live language switching with angular i18n? 有没有办法用角度i18n进行实时语言切换?

Actaully, there is. 实际上,有。 You need to build different dist of your app, but then you can switch live after the full deploy: 您需要构建应用程序的不同dist,但是在完全部署后您可以切换实时:

Official Angluar docs and suggested tutorial 官方Angluar文档建议教程

It is understandable why developers like an ngx-translate like library to take care of internationalization. 为什么开发人员喜欢像ngx-translate这样的库来处理国际化,这是可以理解的。 After all it makes our lives so easy by making the translation problem a 1v1 mapping. 毕竟,通过使翻译问题成为1v1映射,它使我们的生活变得如此简单。 Unfortunately this is not how it works with human languages. 不幸的是,这不适用于人类语言。 One has two know more than one language to better understand the shortcomings of this approach. 一个人有两种语言可以更好地理解这种方法的缺点。

Here's a little example: Say you have an app for travel expense, you have a tabular view in which one column title is "times" indicating when the expense was reported. 这里有一个小例子:假设您有旅行费用的应用程序,您有一个表格视图,其中一列标题是“时间”,表示报告费用的时间。 Then imagine in such app you have a mini calculator for doing basic verification of your expenses which happens to have a multiplication button x with a layover saying "times". 然后想象在这样的应用程序中,你有一个迷你计算器,用于对你的费用进行基本验证,恰好有一个乘法按钮x,停留时间为“时间”。 When you do ngx-translate you extract them both with the same key "TIMES" which in turn the translator guy gives you back one translation for. 当您执行ngx-translate时,您可以使用相同的键“TIMES”将它们提取出来,然后翻译人员会为您提供一个翻译。 But the first occurrence of "time" is not necessarily translated the same as the second one in all the other languages. 但是第一次出现的“时间”并不一定与所有其他语言中的第二次出现相同。 Take Spanish for instance: 以西班牙语为例:

  • "two TIMES three"(as in the calculator) -> "dos POR tres" “两个时间三”(如在计算器中) - >“dos POR tres”
  • "expense TIMES"(as in tabular view) -> "TIEMPOS de gasto" “费用时间”(如表格视图) - >“TIEMPOS de gasto”

This is actually why internationalization is moving to the direction of using a more sophisticated format such as XLF to support for meaning, description(in the case of Angular) rather the old 1-depth JSON style which cannot accommodate for translation with respect to context. 这实际上是为什么国际化正朝着使用更复杂的格式(如XLF)来支持意义,描述(在Angular的情况下)而不是旧的1深度JSON样式的原因,这种样式无法适应关于上下文的翻译。

Now you may argue that this is solvable by registering two different keys for "times" which in English they happen to map to the same thing but that requires you as a developer to know all the languages your app supports while developing or you would have to go through another iteration(time is money!) of getting customer's feedback and then adding a separate key whereas if you provide description and meaning for your message(text) then the translator takes care of it for you without you having any knowledge of the other language(If you know Spanish think about how complicated this can be with subjunctive and indicative forms of verb which are the same in English but not Spanish). 现在你可能会认为这可以通过为“时间”注册两个不同的键来解决,这些键在英语中恰好映射到相同的东西,但这需要你作为开发人员知道你的应用程序在开发时支持的所有语言,否则你将不得不通过另一次迭代(时间就是金钱!)来获取客户的反馈,然后添加一个单独的密钥,而如果您为您的消息(文本)提供描述和含义,那么翻译人员将为您处理它,而您无需了解其他语言(如果你知道西班牙语会想到这与动词的虚拟和指示形式有多么复杂,这些形式在英语中是相同的而不是西班牙语)。

To answer your other question "Is there a way to do live language switching with angular i18n?": yes there is. 要回答你的另一个问题“有没有办法用角度i18n进行实时语言切换?”:是的。 Have a look at this awesome article about state management in apps. 看看这篇关于应用程序中的状态管理的精彩文章 Long story short you need to have your client and persistent states reflected in URL. 简而言之,您需要将您的客户端和持久状态反映在URL中。 Then all you need to do is to add a locale prefix to your path which causes your web server giving you the right locale build. 然后,您需要做的就是在路径中添加一个区域设置前缀,这会使您的Web服务器为您提供正确的区域设置构建。 Then whatever your app state was before changing locale change action happened is recoverable from URL(since it "reflects both the persistent and client state"). 然后,在更改区域设置之前,无论您的应用程序状态如何,都可以从URL中恢复更改操作(因为它“反映了持久性和客户端状态”)。

One of the great advantage of Angular I18N is that its affect to templates is minimal. Angular I18N的一大优势是它对模板的影响很小。 You only have to add i18n attribute on each element you want to localize. 您只需要在要本地化的每个元素上添加i18n属性。 So 所以

<p>Hello World<p>

becomes

<p i18n>Hello World<p>

No need to change the markup a lot and no need to manually maintain the resource file. 无需更改标记,也无需手动维护资源文件。 If you use any other I18N library for Angular or React you need to modify you markup a lot such as 如果您使用任何其他I18N库用于Angular或React,您需要修改许多标记,例如

<p>Translate("Hello World")<p>

and you have to manually add the string into the neutral resource file such as 并且您必须手动将字符串添加到中性资源文件中,例如

"Hello World": "Hello World"

Then if you want to change the string you also have to remember to update the key and value in the resource file. 然后,如果要更改字符串,还必须记住更新资源文件中的键和值。

With Angular I18N you use extract tool to create and maintain the neutral resource files. 使用Angular I18N,您可以使用提取工具来创建和维护中性资源文件。

What is currently missing in Angular I18N is the ability to localize string in source code. Angular I18N目前缺少的是能够在源代码中本地化字符串。 However this feature will appear soon. 但是这个功能很快就会出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 在 i18n 中的 ngx-translate 中翻译一个数字 - Angular Translate a number in ngx-translate in i18n ngx-translate 与 i18n 的区别 - Differences ngx-translate vs i18n Angular国际化(ngx-translate / i18n或i18next) - Angular Internationalization ( ngx-translate/ i18n OR i18next ) NGX-Translate on Angular 4,错误打开“返回新的TranslateHttpLoader(http,&#39;./assets/i18n/&#39;,&#39;.json&#39;);” - NGX-Translate on Angular 4, ERROR ON “return new TranslateHttpLoader(http, './assets/i18n/', '.json');” 可以在使用 Angular 8 的 ngx-translate i18n json 文件中使用正则表达式吗? - Can using regular expression in ngx-translate i18n json file using Angular 8? 当 url 不可用时如何设置回退 (assets/i18n/en.json) (ngx-translate/http-loader) - How to set a fallback (assets/i18n/en.json) when url is not available (ngx-translate/http-loader) 角度5 i18n翻译403 - angular 5 i18n translate 403 Angular2和webpack - i18n插件与ng2-translate - Angular2 and webpack - i18n plugin vs ng2-translate Angular i18n AOT 编译 - 使用 nginx 进行 CI 部署与使用 ng serve 进行本地开发 - Angular i18n AOT compilation - CI deployment with nginx vs. local development with ng serve 使用 ngx-translate 翻译 Angular - Translate Angular with ngx-translate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM