- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我们必须等到 Angular 6 才能让 angular-i18n 支持错误消息等代码的翻译。
对于那些使用 angular-i18n(而不是例如 ngx-translate)的人,您同时在做什么来处理代码中的翻译?我突然想到,如果字符串不多,那么一个简单的语言服务可以使用通过语言代码和 id 获取翻译的方法,但我对更优雅和“有 Angular ”的东西感兴趣。
我不知道 promise 的代码翻译支持会是什么样子,但理想情况下,任何临时解决方案在上线时都可以轻松转换为 angular-i18n 方式。
人们正在做什么来处理这个问题?有什么想法吗?
最佳答案
这个 polyfill 似乎是目前最好的方式:
https://github.com/ngx-translate/i18n-polyfill
它允许您将要翻译的任何内容包装在 i18n()
函数中(此 API 可能会保留在 Angular 的 future 版本中 - 请参阅我在该答案底部的注释).
polyfill 主要由负责 i18n 的 Angular 团队成员 Olivier Combe 编写:
对于 Angular 5,安装时需要版本 0.2.0:
npm install @ngx-translate/i18n-polyfill@0.2.0 --save
对于 Angular 6,获取最新版本 - 当前为 1.0.0:
npm install @ngx-translate/i18n-polyfill@1.0.0 --save
我得到了适用于 Angular 5 的 polyfill JIT 和 AOT 编译(它也适用于 Angular 6)。以下是翻译成一种语言所需要做的事情(这是实现此目的的好方法 - 稍后您可以让多种语言工作,我将在下面进一步解释):
将以下导入添加到根 Angular 模块:
import { TRANSLATIONS, TRANSLATIONS_FORMAT } from '@angular/core';
import { I18n } from '@ngx-translate/i18n-polyfill';
添加以下常量,并在您的根模块中指定提供程序:
// add this after import + export statements
// you need to specify the location for your translations file
// this is the translations file that will be used for translations in .ts files
const translations = require(`raw-loader!../locale/messages.fr.xlf`);
@NgModule({ ....
providers:
[
I18n,
{provide: TRANSLATIONS, useValue: translations},
{provide: TRANSLATIONS_FORMAT, useValue: 'xlf'},
...
Note on using AOT compilation: If you're using AOT compilation to translate your templates, translation of the messages in .ts files will still be done at runtime using JIT compilation (that's why you need to reference
TRANSLATIONS
andTRANSLATIONS_FORMAT
instead of just specifying these in your build scripts).
在要提供翻译的 .ts 文件中,添加以下内容:
import { I18n } from '@ngx-translate/i18n-polyfill';
constructor(private i18n: I18n) {
console.log(i18n("This is a test {{myVar}} !", {myVar: "^_^"}));
}
这表明您甚至可以在要翻译的消息中包含插值。
您可以像这样使用 i18n 定义(即使用指定翻译“源”id、含义、描述):
this.i18n({value: 'Some message', id: 'Some message id', meaning: 'Meaning of some message', description: 'Description of some message'})
您仍然需要提取消息,您可以使用 ngx-extractor 工具来执行此操作。这在您安装 polyfill 时包含在内,我在下面添加了一个示例,说明它在 npm 脚本中的用法。另请参阅 polyfill page 上的自述文件.
要支持在多种语言之间切换,您需要一个工厂提供程序来进行翻译。 polyfill page 的自述文件中有详细信息.您需要在根模块中使用类似这样的东西(或者对于 AOT 编译,将 localeFactory
的返回值替换为检测您的应用程序当前运行的 AOT 编译语言变体的函数):
export function localeFactory(): string {
return (window.clientInformation && window.clientInformation.language) || window.navigator.language;
}
providers:
[
{
provide: TRANSLATIONS,
useFactory: (locale) => {
locale = locale || 'en'; // default to english if no locale provided
return require(`raw-loader!../locale/messages.${locale}.xlf`);
},
deps: [LOCALE_ID]
},
{
provide: LOCALE_ID,
useFactory: localeFactory
},
所有这些都与 xliffmerge 兼容,这是一个很好的工具,可以自动合并您添加的任何新翻译,而不会覆盖现有翻译。 Xliffmerge 还可以使用谷歌翻译自动执行翻译(您需要谷歌翻译 API key )。为此,我按以下顺序进行提取和合并/翻译,在我进行实际的 AOT 构建之前:
"extract-i18n-template-messages": "ng xi18n --outputPath=src/locale --i18n-format=xlf",
"extract-i18n-ts-messages": "ngx-extractor --input=\"src/**/*.ts\" --format=xlf --out-file=src/locale/messages.xlf",
"generate-new-translations": "xliffmerge --profile xliffmerge.json en fr es de zh"
网站特定语言版本的 AOT 构建如下所示:
"build:fr": "ng build --aot --output-path=dist/fr --base-href /fr/ --i18nFile=src/locale/messages.fr.xlf --i18nFormat=xlf --locale=fr",
本文主要由负责国际化的Angular团队成员Olivier Combe编写。在这个阶段,这是一个“推测性”的 polyfill,用于翻译 .ts 文件中的变量或字符串。它很可能会被 Angular 中内置的 API 所取代,这将非常相似,因此以后的升级应该是可以合理管理的。这是来自 Github 页面的免责声明:
This library is a speculative polyfill, it means that it's supposed to replace an API that is coming in the future. If the API is different, a migration tool will be provided if it's possible and necessary.
关于在即将推出的 Angular 6 次要版本中对代码中变量/字符串的翻译的支持存在一些讨论。
这是来自 Olivier Combe(今年 3 月)的引述,来自 Github 上的以下讨论:
https://github.com/angular/angular/issues/11405
The first PR for runtime i18n has been merged into master, along with a hello world demo app that we will use to test the features. It works at runtime, and support theoretically code translations, even if there is no service for it yet. For now it's very minimal support (static strings), we're working on adding new features (I'll make the extraction work next week, and then dynamic string with placeholders and variables). After that we'll do the service for code translations. As soon as a new feature is finished it gets merged into master, you won't have to wait for a new major.
关于angular-i18n 解决代码翻译问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48327401/
我尝试理解[c代码 -> 汇编]代码 void node::Check( data & _data1, vector& _data2) { -> push ebp -> mov ebp,esp ->
我需要在当前表单(代码)的上下文中运行文本文件中的代码。其中一项要求是让代码创建新控件并将其添加到当前窗体。 例如,在Form1.cs中: using System.Windows.Forms; ..
我有此 C++ 代码并将其转换为 C# (.net Framework 4) 代码。有没有人给我一些关于 malloc、free 和 sprintf 方法的提示? int monate = ee; d
我的网络服务器代码有问题 #include #include #include #include #include #include #include int
给定以下 html 代码,将列表中的第三个元素(即“美丽”一词)以斜体显示的 CSS 代码是什么?当然,我可以给这个元素一个 id 或一个 class,但 html 代码必须保持不变。谢谢
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我试图制作一个宏来避免重复代码和注释。 我试过这个: #define GrowOnPage(any Page, any Component) Component.Width := Page.Surfa
我正在尝试将我的旧 C++ 代码“翻译”成头条新闻所暗示的 C# 代码。问题是我是 C# 中的新手,并不是所有的东西都像 C++ 中那样。在 C++ 中这些解决方案运行良好,但在 C# 中只是不能。我
在 Windows 10 上工作,R 语言的格式化程序似乎没有在 Visual Studio Code 中完成它的工作。我试过R support for Visual Studio Code和 R-T
我正在处理一些报告(计数),我必须获取不同参数的计数。非常简单但乏味。 一个参数的示例查询: qCountsEmployee = ( "select count(*) from %s wher
最近几天我尝试从 d00m 调试网络错误。我开始用尽想法/线索,我希望其他 SO 用户拥有可能有用的宝贵经验。我希望能够提供所有相关信息,但我个人无法控制服务器环境。 整个事情始于用户注意到我们应用程
我有一个 app.js 文件,其中包含如下 dojo amd 模式代码: require(["dojo/dom", ..], function(dom){ dom.byId('someId').i
我对“-gencode”语句中的“code=sm_X”选项有点困惑。 一个例子:NVCC 编译器选项有什么作用 -gencode arch=compute_13,code=sm_13 嵌入库中? 只有
我为我的表格使用 X-editable 框架。 但是我有一些问题。 $(document).ready(function() { $('.access').editable({
我一直在通过本教程学习 flask/python http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-wo
我想将 Vim 和 EMACS 用于 CNC、G 代码和 M 代码。 Vim 或 EMACS 是否有任何语法或模式来处理这种类型的代码? 最佳答案 一些快速搜索使我找到了 this vim 和 thi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve this
这个问题在这里已经有了答案: Enabling markdown highlighting in Vim (5 个回答) 6年前关闭。 当我在 Vim 中编辑包含 Markdown 代码的 READM
我正在 Swift3 iOS 中开发视频应用程序。基本上我必须将视频 Assets 和音频与淡入淡出效果合并为一个并将其保存到 iPhone 画廊。为此,我使用以下方法: private func d
pipeline { agent any stages { stage('Build') { steps { e
我是一名优秀的程序员,十分优秀!