Angular 5 Breaking change - manually import locale

后端 未结 3 777
误落风尘
误落风尘 2020-12-08 04:29

Changelog says:

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 wi

3条回答
  •  执笔经年
    2020-12-08 05:00

    Angular pipes can help you with internationalization: the DatePipe, CurrencyPipe, DecimalPipe and PercentPipe use locale data to format data based on the LOCALE_ID.

    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:

    src/app/app.module.ts
    content_copy
    import { registerLocaleData } from '@angular/common';
    import localeFr from '@angular/common/locales/fr';
    
    registerLocaleData(localeFr);
    

    The files in @angular/common/locales contain most of the locale data that you need, but some advanced formatting options might only be available in the extra dataset that you can import from @angular/common/locales/extra. An error message informs you when this is the case.

    src/app/app.module.ts
    content_copy
    import { registerLocaleData } from '@angular/common';
    import localeFrCa from '@angular/common/locales/fr-CA';
    import localeFrCaExtra from '@angular/common/locales/extra/fr-CA';
    
    registerLocaleData(localeFrCa, localeFrCaExtra);
    

    See the table about old and new locale too

提交回复
热议问题