如何将 npx bootstrap 日期选择器语言更改为 pt-br?

How do I change the npx bootstrap datepicker language to pt-br?

我在 Angular 4 做一个项目,我安装了 ngx bootstrap 并且日期选择器是英文的。我在 app.module.ts 中插入了以下行:

import { defineLocale } from 'ngx-bootstrap/chronos';
import { ptBrLocale } from 'ngx-bootstrap/locale';
defineLocale('pt-br', ptBrLocale); 

然后我进入调用日期选择器的页面组件,进行导入并声明:

import { Component, OnInit } from '@angular/core';
import { BsDatepickerConfig, BsLocaleService } from 'ngx-bootstrap/datepicker';
import { listLocales } from 'ngx-bootstrap/chronos';

@Component({
  selector: 'demo-datepicker-change-locale',
  templateUrl: './cancelar-agendamentos-consulta.component.html'
})
export class DemoDatepickerChangeLocaleComponent {
  locale = 'pt-br';
  locales = listLocales();

  constructor(private _localeService: BsLocaleService) {
  }

  applyLocale(pop: any) {
    this._localeService.use(this.locale);
    pop.hide();
    pop.show();
  }
}

还是不行。我做错了什么?

您的代码存在问题,您将更改区域设置的调用放在另一个函数中,而您没有调用它。

尝试删除 applyLocale 函数并将更改语言环境的调用放在构造函数中,如下所示:

@Component({
 selector: 'demo-datepicker-change-locale',
 templateUrl: './cancelar-agendamentos-consulta.component.html'
})
export class DemoDatepickerChangeLocaleComponent {
  locale = 'pt-br';
  locales = listLocales();

  constructor(private _localeService: BsLocaleService) {
    this._localeService.use(this.locale);
  }
}

所以我希望语言是意大利语,我用这段代码更改了语言:

我导入了:

import { BsLocaleService, defineLocale, itLocale } from 'ngx-bootstrap';

example.component.ts中的代码

  // set language of datepicker
  setDatepickerLanguage() {
    defineLocale('it', itLocale);
    this.localeService.use('it');
  }

并在构造函数上调用函数() 它奏效了。

截图: