Angular 9 introduced a global '$localize()' function that needs to be loaded

I'm getting the following error in my new angular project setup.

Installed Packages and its versions

ERROR Error: Uncaught (in promise): 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 add import '@angular/localize'; to your polyfills.ts file. 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.

Note: I came from the following. It suggests falling back to old version. https://github.com/angular/angular/issues/32508

67827 次浏览

You need to make sure you have the @angular/localize package first:

npm install @angular/localize --save

Then, import '@angular/localize/init' in your polyfills.ts file just like the error says

Angular 9 introduced a global $localize() function that needs to be loaded if you use i18n.

Run ng add @angular/localize from the Angular CLI.

Then make sure you have:
- @angular/localize as a dependency in your app's package.json
- import '@angular/localize/init' in your polyfills.ts

As the i18n attributes are now converted to $localize calls in the generated code, we need to load the $localize function.

The CLI offers a schematic to do this for you. Simply run: ng add @angular/localize

It will add the package to your dependencies and the necessary import to your polyfills (import '@angular/localize/init')

You can also refer to the following link for more explanation. https://blog.ninja-squad.com/2019/12/10/angular-localize/

The best way if you are using Angular CLI is to run

ng add @angular/localize

It will take care of it automatically

Or else

import '@angular/localize/init'; to your polyfills.ts

Tested with Angular 9

If you have many angular projects in same workspace, running ng add @angular/localize will add import statement import '@angular/localize/init' to polyfills.ts in default Project only,i think this will be fixed in later updates.

so you need to add import '@angular/localize/init' manually to polyfills.ts in other projects.

If you are running ng test and the above answer doesn't work, install the package and add

import "@angular/localize/init"

to polyfills-test.ts

The best way to do this is by using Angular CLI:

You just have to run the following command in terminal:

ng add @angular/localize

It will automatically install packages and will also add the import statement in polyfills.ts file.

The import statement is:

import '@angular/localize/init';

If you don't want to go through the CLI approach then you can manually enter the import statement in the polyfills.ts file. Also you have to do one more step is to add the below line in package.json under dependencies tag.

"dependencies":{
...
"@angular/localize":"^9.1.0",
...
}

This error started to appear after I upgraded a large Nx/Angular CLI application with multiple sub apps to Angular 10. The solution suggested in the error (Please run ng add @angular/localize from the Angular CLI) does not work if the application contains multiple apps. Each of these auto-generated app had its own polyfill.ts. I manually added the import (import '@angular/localize/init';) in each of the polyfill.ts file. To fix same error while running unit tests, I also had to add the import in test.ts file of libs.

You have to add this package

ng add @angular/localize

then add import '@angular/localize/init'; to your ployfills.ts

But if your tests that are failing in a library, you must add import '@angular/localize/init'; to your test.ts

run

npm install @angular/localize --save

Then, in your polyfills.ts

import '@angular/localize/init'

this works for me

I changed my operating system because of this problem. And yesterday I found the cause of this problem. I was getting this issue due to a plugin I installed in Visual Studio Code. If you are using Angular9, do not install the first two plugins. You can install the third one.

enter image description here

For me this error appeared when running npm test so the solutions above did not help. To solve it I had to import localize in the jest.ts file. Just add:

import "@angular/localize/init";

I had this error while updating Angular version from 12 to 13. I followed the instructions:

  1. Run ng add @angular/localize
  2. import '@angular/localize/init' in your polyfills.ts

but it did not work for me. I already had @angular/localize in my modules and the import in the polyfills.ts file, after all my Angular 12 app was working fine.

To solve the issue, I had to update the "@ng-bootstrap/ng-bootstrap" version. I hope this suggestion can save some headache to someone, given that there was no apparent link between the outdated @ng-bootstrap/ng-bootstrap version and the error description.

replace this in polyfil:

import '../node_modules/@angular/localize/init';

with this :

import '@angular/localize/init';

and everything is OK !