Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make translations for NgbDatepicker in Angular without subclassing NgbDatepickerI18n?

I am currently reading about internationalization of the ng-bootstrap components. Their documentation says for the datepicker:

Since the 2.0.0 release datepicker will use the application locale if it is present to get translations of weekdays and month names. The internal service that does translation is called NgbDatepickerI18n and you could provide your own implementation if necessary.

(ng-bootstrap website)

Looking at the Angular i18n documentation, it states:

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

src/app/app.module.ts

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

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

(Angular website)

But why is it not working for me? Do I still have to make a custom implementation of NgbDatepickerI18n, or am I missing something?


Here is an example playground:

https://stackblitz.com/edit/angular-mezpyn?file=app%2Fdatepicker-popup.module.ts

like image 357
andreas Avatar asked Apr 05 '19 14:04

andreas


People also ask

How to change the title of angulardatepicker project in Visual Studio Code?

Now we open ANGULARDATEPICKER project inside VS Code. Select OpenFolder option to open the folder files in VISUAL STUDIO CODE. Expand folder SRC and double click on app.component.ts file. Now switch to app.component.html file where we write html code. As you can see the below screenshot title has been changed successfully. Welcome to { { title }}!

How to set date value for ngbdatepicker?

To set value for ngbDatepicker you need set with format object as below: var setDate = { year: 2020, month: 10, day: 10 }; In your case, you can do as below :

How do I translate an angular app into another language?

With ngx-translate it's easy to create a multilingual version of your Angular app. Use ngx-translate-extract to keep your translation files up-to-date. Finally BabelEdit helps you to mange and edit your translations. The source code is available on GitHub .

How do I use the translate pipe in angular?

You can use the translate pipe much like you’d use any other pipe in Angular. The input into the pipe is the key of the translation you need. The optional parameter is an object which defines any interpolation strings that the translation is expecting. In the example below, the component has a user object, with a property of firstName:


1 Answers

I had the same problem and becuase there's no answer, this is the solution:

After importing and registering the locale data as you did, in app.module.ts you need to add

providers: [
    { provide: LOCALE_ID, useValue: 'fr-FR' },
    ... other providers
],

The documentation doesn't make it clear in my opinion.

like image 78
alex Avatar answered Sep 28 '22 12:09

alex