Angular在从版本14更新到版本15后出现$localize错误。

huangapple go评论52阅读模式
英文:

Angular $localize error after updating from version 14 to 15

问题

抱歉,由于您要求只翻译代码部分,以下是您提供的内容的代码部分翻译:

After updating our Angular app from version **14.0.4** to version **15.1.3** (also tried 15.1.2 before) we get the following error when trying to access the app:

Uncaught Error: It looks like your application or one of its dependencies is using i18n. 
Angular 9 introduced a global `$localize()` function that needs to be loaded.
Please run `ng add @angular/localize` from the Angular CLI.
(For non-CLI projects, add `import '@angular/localize/init';` to your `polyfills.ts` file.
For server-side rendering applications add the import to your `main.server.ts` file.)

So the problem is, that we already did those steps many updates ago. As you can see also from the description, this is sth. introduced with version 9. But we are running version 14 (and earlier) without any issues of this kind. The app compiles without errors and this is a run-time error.

I also tried re-running the mentioned command `ng add @angular/localize` after the version update, which just adds `@angular/localize` to the types array in the tsconfig, but it still fails with the same error.

I did the update following the recommended angular update website (https://update.angular.io/?l=3&v=14.0-15.0) and also updated all other co-dependencies to the newest version (like material, NgRx, typescript etc.)

We use `$localize` either directly in components, for example: 

 private readonly onLabel = $localize`:@@common_onLabel:`;

Or also in a shared way like this: 

export const i18nD = $localize`:@@components_timeDisplay_shortDaysFormat:` as 'd'

I would appreciate any ideas or steps I could try, because I'm running out of ideas what to try next.

如果您有任何其他问题或需要进一步的帮助,请随时提问。

英文:

After updating our Angular app from version 14.0.4 to version 15.1.3 (also tried 15.1.2 before) we get the following error when trying to access the app:

Uncaught Error: It looks like your application or one of its dependencies is using i18n. 
Angular 9 introduced a global `$localize()` function that needs to be loaded.
Please run `ng add @angular/localize` from the Angular CLI.
(For non-CLI projects, add `import '@angular/localize/init';` to your `polyfills.ts` file.
For server-side rendering applications add the import to your `main.server.ts` file.)

So the problem is, that we already did those steps many updates ago. As you can see also from the description, this is sth. introduced with version 9. But we are running version 14 (and earlier) without any issues of this kind. The app compiles without errors and this is a run-time error.

I also tried re-running the mentioned command ng add @angular/localize after the version update, which just adds @angular/localize to the types array in the tsconfig, but it still fails with the same error.

I did the update following the recommended angular update website (https://update.angular.io/?l=3&v=14.0-15.0) and also updated all other co-dependencies to the newest version (like material, NgRx, typescript etc.)

We use $localize either directly in components, for example:

 private readonly onLabel = $localize`:@@common_onLabel:`;

Or also in a shared way like this:

export const i18nD = $localize`:@@components_timeDisplay_shortDaysFormat:` as 'd';

I would appreciate any ideas or steps I could try, because I'm running out of ideas what to try next.

答案1

得分: 2

看起来在最近的版本中,他们使用了一个不同的系统来包含全局的 $localize 函数。它不再依赖于 polyfill,而是在 TS 编译器配置的 types 数组中查找 @angular/localize 的存在。你可以尝试重新运行用于添加 $localize 的原理图,使用以下命令:

ng add @angular/localize

或者在你的 tsconfig.json 或你的项目所使用的其他 TS 配置文件中手动添加类型路径,如下所示:

{
  "compilerOptions": {
    ...
    "types": ["@angular/localize"]
  }
  ...
}
英文:

It looks like in recent versions they use a different system to include global $localize function.
It does not rely anymore on polyfill, but instead looks for the presence of @angular/localize among TS compiler configuration types array's items.
https://github.com/angular/angular-cli/pull/24032

Try re-issuing schematic for adding $localize

ng add @angular/localize

or adding manually the type path in your tsconfig.json or whatever TS configuration file your project uses.

{
  "compilerOptions": {
    ...
    "types": ["@angular/localize"]  
  }
  ...
}

答案2

得分: 2

我终于找到问题所在了:它是一个过时的 browserslist 配置。
这个配置已经多年没有更新了,看起来是这样的:

"browserslist": [
  "> 1%",
  "last 1 version",
  "not ie > 0",
  "not ie_mob > 0",
  "safari >= 10",
  "not dead"
]

完全移除它已经解决了问题。我仍在评估是否应该将它设置为这个 "defaults and supports es6-module" 或者 Angular 15 的默认配置:https://angular.io/guide/build#configuring-browser-compatibility。两者似乎都能正常工作,不会引起我在问题中提到的错误。

英文:

I finally found the issue: It was an outdated browserslist configuration.
It was not touched for several years and looked like this:

"browserslist": [
  "> 1%",
  "last 1 version",
  "not ie > 0",
  "not ie_mob > 0",
  "safari >= 10",
  "not dead"
]

Removing it completely already fixed the issue. I'm still evaluating if I should set it to this "defaults and supports es6-module" or the Angular 15 defaults: https://angular.io/guide/build#configuring-browser-compatibility. Both seem to work and not cause the error mentioned in my question.

答案3

得分: 0

我在以下代码中遇到了错误:

last 5 Chrome versions
last 5 Firefox versions
last 5 Edge major versions
last 5 Safari major versions

但在以下代码中没有问题:

chrome > 60
last 4 Chrome versions
last 4 Firefox versions
last 4 Edge major versions
last 4 Safari major versions

我已经安装并导入了@angular/localize

我尝试使用大于5的数字,但以失败告终。

英文:

I got error with

last 5 Chrome versions
last 5 Firefox versions
last 5 Edge major versions
last 5 Safari major versions

but okay with

chrome > 60
last 4 Chrome versions
last 4 Firefox versions
last 4 Edge major versions
last 4 Safari major versions

I do have the @angular/localize installed and imported.

I tried number more than 5, it ends with failure.

huangapple
  • 本文由 发表于 2023年2月7日 00:43:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定