【问题标题】:How do I change the npx bootstrap datepicker language to pt-br?如何将 npx 引导日期选择器语言更改为 pt-br?
【发布时间】:2023-03-17 07:48:01
【问题描述】:

我在 Angular 4 中做一个项目,我安装了 ngx 引导程序,日期选择器是英文的。我在 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();
  }
}

它仍然没有工作。我做错了什么?

【问题讨论】:

    标签: angular typescript datepicker components ngx-bootstrap


    【解决方案1】:

    您的代码的问题是您将更改语言环境的调用放在另一个函数中,而您没有调用它。

    尝试删除 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);
      }
    }
    

    【讨论】:

      【解决方案2】:

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

      我导入了:

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

      example.component.ts 中的代码

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

      并在constructor()上调用函数 并且成功了。

      截图:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        • 1970-01-01
        • 2019-08-23
        相关资源
        最近更新 更多