question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular 5 - manually importing locale doesnt work

See original GitHub issue

Since angular 5 there has been a “breaking change” to importing additional locales:

By default Angular now only contains locale data for the language en-US, if you set the value of LOCALE_ID to another locale, you will have to import new locale data for this language because we don’t use the intl API anymore.

Before angular 5, just having these 2 lines was enough to use for example Date filter with custom locale:

import { LOCALE_ID } from '@angular/core';
...
{ provide: LOCALE_ID, useValue: 'de-CH' }
...

but now as we see in docs: https://angular.io/guide/i18n#i18n-pipes1

By default, Angular only contains locale data for en-US. If you set the value of LOCALE_ID to another locale, you must import locale data for that new locale. The CLI imports the locale data for you when you use the parameter --locale with ng serve and ng build.

If you want to import locale data for other languages, you can do it manually:

import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';

// the second parameter 'fr' is optional
registerLocaleData(localeFr, 'fr');

But if i try to do that gives an error:

System.err: SyntaxError: Unexpected token export
System.err: File: "file:///data/data/com.example.app/files/app/tns_modul
es/@angular/common/locales/fr.js, line: 10, column: 0

This is a big issue

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
hettigercommented, Jan 8, 2018

Example workaround for de-DE until there’s a official solution:

  1. Copy the file node_modules/@angular/common/locales/de.js to app/de.ts
  2. In your app.module.ts file use the following code:
// TODO: Use following import instead and remove ./de.ts once https://github.com/angular/angular/issues/20605 is fixed
// import localeDe from "@angular/common/locales/de";
import { localeDe } from "./de";
registerLocaleData(localeDe);
  1. Set LOCALE_ID in the providers array: { provide: LOCALE_ID, useValue: 'de' },

Of course this workaround can be applied to any locale… Keep in mind this might cause issues whenever you update your dependencies.

1reaction
xavierluzcommented, May 30, 2018

@hettiger

thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 5 Breaking change - manually import locale
The CLI imports the locale data for you when you use the parameter --locale with ng serve and ng build.
Read more >
Set the runtime locale manually - Angular
To manually set the runtime locale of an application to one other than the automatic value, complete the following actions.
Read more >
Import global variants of the locale data - Angular
Import global variants of the locale datalink ... The Angular CLI automatically includes locale data if you run the ng build command with...
Read more >
$localize Global Import Migration - Angular
@angular/localize is a new package that supports i18n of messages in Ivy applications. This package requires a global $localize symbol to exist. The...
Read more >
Component testing scenarios - Angular
Here are three tests that illustrate how automatic change detection works. ... The Angular testing environment does not know that the test changed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found