78

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

Bill P
  • 3,612
  • 10
  • 19
  • 31
  • What is your question, exactly? The error message gives clear instructions on what to do. – JJJ Sep 16 '19 at 11:33

13 Answers13

116

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

Saurabh Mistry
  • 11,077
  • 4
  • 41
  • 64
rhino5oh
  • 1,264
  • 1
  • 10
  • 8
  • I get this error when running unit tests with Jest after adding a postinstall step for ngcc, as suggested by this answer - https://stackoverflow.com/a/63727427. @angular/localize is a dependency in my package.json, and the @angular/localize/init is an import in my polyfills.ts, but same error on most test suites. I am running Angular 9. Any suggestions on how to resolve? – plantbeard Jun 15 '21 at 18:50
  • 2
    Meh sorry just found the answer if anyone else has this issue - https://github.com/just-jeb/angular-builders/issues/678#issuecomment-755903805 – plantbeard Jun 15 '21 at 18:56
  • For me I needed to use the `--skip-confirmation` flag: `npm install @angular/localize --skip-confirmation --save`. Otherwise I got [an error](https://github.com/angular/angular-cli/issues/21512) "No terminal detected. '`--skip-confirmation`' can be used to bypass installation confirmation. Ensure package name is correct prior to '`--skip-confirmation`' option usage." – The Red Pea Nov 22 '21 at 16:16
43

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

dota2pro
  • 6,421
  • 7
  • 34
  • 68
15

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.

6

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

Vaishak
  • 87
  • 3
  • 12
5

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

xidedix
  • 206
  • 1
  • 7
5

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.

Atif Majeed
  • 901
  • 13
  • 13
  • I wish this answer was higher, this was exactly the problem I had with inner libs and had to add the import in the test.ts or it won't budge. – Zentaurus Jun 05 '21 at 02:19
3

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/

Anshita Singh
  • 1,084
  • 1
  • 8
  • 28
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – THess Jan 14 '20 at 07:16
  • @THess thanks for the suggestion, I have updated the answer and shared the link for reference. – Anshita Singh Jan 14 '20 at 07:48
3

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",
    ...    
}
Marco Bonelli
  • 55,971
  • 20
  • 106
  • 115
2

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

Hicham
  • 558
  • 4
  • 16
2

run

npm install @angular/localize --save

Then, in your polyfills.ts

import '@angular/localize/init' 

this works for me

Syscall
  • 18,131
  • 10
  • 32
  • 49
jayanga
  • 45
  • 1
2

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

Furkan Gulsen
  • 977
  • 1
  • 6
  • 18
2

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.

Luigi Rubino
  • 514
  • 1
  • 6
  • 16
1

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";