我正在将我的 Angular 11 应用程序重构为库。我的常规应用程序是本地化的,@angular/localize用作开发依赖项,因此我$localize在我的部分代码中使用了该指令。我需要将带有此指令的代码移动到库中,但我不知道如何执行此操作。
在我的库package.json文件中,我添加了一个 peer-dependency on@angular/localize以匹配我的应用程序中的 devDependency:
{
"name": "example",
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "^11.0.0",
"@angular/core": "^11.0.0",
"@angular/localize": "~11.0.0"
},
"dependencies": {
"tslib": "^2.0.0"
}
}
Run Code Online (Sandbox Code Playgroud)
但是,编译器报告说,$localize当我尝试执行ng build <library name>.
× Compiling TypeScript sources through NGC
ERROR: projects/example/src/lib/example.service.ts:52:23 - error TS2304: Cannot find name '$localize'.
52 description = $localize `example localized text`;
~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
如何$localize在我的库中使用该指令?
>ng version
_ _ ____ _ ___
/ \ _ …Run Code Online (Sandbox Code Playgroud) 所以我有一个 Angular 11 应用程序,我刚刚使用 Angulars 默认的 i18n 中间件实现了本地化。
要创建一个初始文件,我刚刚运行:
ng extract-i18n myprojectName --format xlf --output-path src/i18n --out-file messages.da.xlf --progress
Run Code Online (Sandbox Code Playgroud)
一切都很好,我得到了一个带有默认语言翻译的 messages.da.xlf 文件。
现在我想将应用程序翻译成英语。
我复制了 messages.da.xlf 文件并将其命名为 messages.en.xlf 并翻译了所有内容。
现在一切都很好。
但现在我有一个问题,下次我添加要翻译的附加文本时,英语翻译不会更新。
所以我的问题是,当我因为应用程序中的新功能而需要扩展翻译时,我如何知道必须将哪些附加“字段”添加到所有翻译文件中,以及是否有办法自动执行此操作?
我正在使用 Angular 的本地化@angular/localize,在配置它以将应用程序翻译成不同的语言后,我尝试使用“ng serve”提供它的默认版本,但出现此错误:
An unhandled exception occurred: The development server only supports localizing a single locale per build.
Run Code Online (Sandbox Code Playgroud)
我基于此尝试使用不同的端口:How to serve different Angular locales during development using ng serve?但无济于事。
对于翻译版本的一切作品用“NG服务--configuration = FR”。构建正常工作,在 dist/App 文件夹中创建“en-US”和“fr”文件夹。
当我在 dev-server 中删除 inlineLocales.size 条件时,一切正常,没有错误:
if (i18n.shouldInline && tsConfig.options.enableIvy !== false) {
//if (i18n.inlineLocales.size > 1) {
// throw new Error('The development server only supports localizing a single locale per build');
//}
await setupLocalize(i18n, browserOptions, webpackConfig);
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习并在项目中添加角度国际化。我只能理解角度文档的编译时翻译(https://angular.io/guide/i18n-overview)。
我需要像这样的东西https://stackblitz.com/github/ngx-translate/example这是第三方库“ngx-translate”。
难道我们不能仅使用 Angular 库而不使用任何第三方库来实现相同的运行时翻译吗?
如果角度库可以进行运行时翻译,请指导我。
internationalization angular-i18n ngx-translate angular angular-localize