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

18

89

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

Adalie answered 16/9, 2019 at 8:48 Comment(1)
What is your question, exactly? The error message gives clear instructions on what to do.Richelieu
G
136

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

Gilletta answered 17/9, 2019 at 4:36 Comment(3)
I get this error when running unit tests with Jest after adding a postinstall step for ngcc, as suggested by this answer - 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?Boger
Meh sorry just found the answer if anyone else has this issue - github.com/just-jeb/angular-builders/issues/…Boger
For me I needed to use the --skip-confirmation flag: npm install @angular/localize --skip-confirmation --save. Otherwise I got an error "No terminal detected. '--skip-confirmation' can be used to bypass installation confirmation. Ensure package name is correct prior to '--skip-confirmation' option usage."Endocardial
H
51

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

Heidy answered 10/2, 2020 at 19:21 Comment(2)
does it automatically add it to the package.json with 'ng add...'?Smithereens
Note: After Angular 14, there is no polyfills file anymore.Craal
S
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.

Synthesize answered 13/2, 2020 at 22:21 Comment(5)
Underrated answer. This is never mentioned on the official guides.Helbonnas
I suspect this answer may have saved my day and my sanity.Azpurua
In my case also ng add didn't add it to polyfills.ts. Thanks for this.Nugatory
This answer saved me days. My default project already had the import, so the CLI report was very misleading: "Skipping installation: Package already installed Nothing to be done."Lesleelesley
Is it possible to know what part/lib of my application depends on i18n?Bolt
P
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.

Placeman answered 23/9, 2020 at 19:19 Comment(2)
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.Woke
Thanks!!!!! This is the answer for in NX!Tuberculate
A
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

Arachnid answered 23/3, 2020 at 9:42 Comment(1)
Good point here, also a setup with a specific ts file for tests. Need to be updated as wellDemisemiquaver
M
6

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

Microdot answered 21/12, 2020 at 15:24 Comment(1)
Im still getting that error, Ive removed the extension and cleaned the cache and %appdata% folder too :(Mcguinness
L
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

Lawerencelawes answered 24/10, 2019 at 9:0 Comment(0)
V
5

run

npm install @angular/localize --save

Then, in your polyfills.ts

import '@angular/localize/init' 

this works for me

Vaticinate answered 16/3, 2021 at 12:29 Comment(0)
C
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.

Cordiecordier answered 11/1, 2022 at 10:5 Comment(0)
R
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",
    ...    
}
Rokach answered 1/4, 2020 at 15:43 Comment(0)
N
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";
Nagual answered 1/1, 2022 at 15:32 Comment(0)
A
3

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/[email protected] 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"
  ]
}
Appendage answered 16/5, 2023 at 21:11 Comment(1)
Yes that is it, it works for Angular 16.2.0. Thank you!Badenpowell
E
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/

Emeritaemeritus answered 14/1, 2020 at 6:55 Comment(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.Felizio
@Felizio thanks for the suggestion, I have updated the answer and shared the link for reference.Emeritaemeritus
D
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

Dahabeah answered 20/7, 2021 at 8:45 Comment(0)
S
1

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

That's it.

Streptococcus answered 25/7, 2023 at 10:42 Comment(0)
S
0

replace this in polyfil:

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

with this :

import '@angular/localize/init';

and everything is OK !

Sectional answered 13/6, 2022 at 13:49 Comment(0)
A
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.

Abscise answered 11/2, 2023 at 12:22 Comment(0)
E
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!

Emee answered 15/5, 2023 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.