88

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,622
  • 10
  • 20
  • 32
  • What is your question, exactly? The error message gives clear instructions on what to do. – JJJ Sep 16 '19 at 11:33

18 Answers18

130

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
  • 12,833
  • 5
  • 50
  • 71
rhino5oh
  • 1,404
  • 1
  • 11
  • 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." – Nate Anderson Nov 22 '21 at 16:16
49

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
  • 7,220
  • 7
  • 44
  • 79
14

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.

8

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
  • 1,021
  • 17
  • 14
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
  • 4
  • 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
  • 210
  • 1
  • 8
5

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
  • 766
  • 7
  • 20
5

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
  • 564
  • 1
  • 7
  • 17
4

run

npm install @angular/localize --save

Then, in your polyfills.ts

import '@angular/localize/init' 

this works for me

Syscall
  • 19,327
  • 10
  • 37
  • 52
jayanga
  • 95
  • 1
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
  • 63,369
  • 21
  • 118
  • 128
Shaikh Nazish
  • 169
  • 1
  • 4
3

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

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,583
  • 1
  • 17
  • 40
  • 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
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
  • 1,384
  • 2
  • 12
  • 24
0

replace this in polyfil:

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

with this :

import '@angular/localize/init';

and everything is OK !

Milad Jafari
  • 970
  • 10
  • 15
0

This error message may appear as well when you are switching to a different i18n library and have not cleaned up the project properly, such as removing all the occurrences of i18n in case you used the default @angular/localize package.

roxhens
  • 508
  • 6
  • 22
0

I had the import '@angular/localize/init'; on the polyfill.ts file but the error was somewhere else! I Just drop it here in case it helps someone!

I have a .browserlistrc file on my project and the set of rules just broke Angular i18n. In my case, i had to remove the >0.3% from the list in order to get it working!

Have a nice day!

Raphaël Roux
  • 316
  • 2
  • 7
0

So, in my case I try to use @ng-bootstrap/ng-bootstrap for Angular v15.

After long investigation, I created new project with Angular v14:

npx -p @angular/cli@14.2.0 new hello-world

and install

@ng-bootstrap/ng-bootstrap

And I worked!

So, I created polyfills.ts in root /src folder.

Then update angular.json:

projects/architect/build/polyfills: ["zone.js", "src/polyfills.ts"],

and

projects/architect/test/polyfills: ["zone.js", "zone.js/testing", "src/polyfills.ts"],

in tsconfig.app.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": [
      "node"
    ]
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts" <- add this
  ],
  "include": [
    "src/**/*.d.ts"
  ]
}

the same in tsconfig.spec.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "types": [
      "jasmine",
      "@angular/localize"
    ]
  },
  "files": [
    "src/polyfills.ts" <- add this
  ],
  "include": [
    "src/**/*.spec.ts",
    "src/**/*.d.ts"
  ]
}
0

If you are using Angular Universal(SSR) then just add import '@angular/localize/init'; on line 1. in server.ts file

That's it.

KARNAV PARGI
  • 445
  • 1
  • 4
  • 15