【问题标题】:Use a ng-bootstrap internationalized datepicker in an input在输入中使用 ng-bootstrap 国际化日期选择器
【发布时间】:2017-12-07 11:08:10
【问题描述】:

我想在输入表单中有一个法语日期选择器。

这里是 ng-bootstrap 示例,有一个“英文”:

<form class="form-inline">
  <div class="form-group">
    <div class="input-group">
      <input class="form-control" placeholder="yyyy-mm-dd"
             name="dp" [(ngModel)]="model" ngbDatepicker #d="ngbDatepicker">
      <button class="input-group-addon" (click)="d.toggle()" type="button">
        <img src="img/calendar-icon.svg" style="width: 1.2rem; height: 1rem; cursor: pointer;"/>
      </button>
    </div>
  </div>
</form>

Here 他们解释了如何使用带有自定义日期选择器的&lt;ngbd-datepicker-i18n&gt;&lt;/ngbd-datepicker-i18n&gt;here 制作法语版本,还有更多关于如何使用它的细节。

但我想要的是使用他们在上面使用的相同的东西,在我的input 标记中使用模板引用变量#d="ngbDatepicker"。类似#d="myCustomNgbDatepicker"

有可能吗?如果是,如何?

【问题讨论】:

    标签: angular datepicker internationalization ng-bootstrap


    【解决方案1】:

    Menna Ramadan 在 ngbDatePicker 文档中添加了指向具有相同结构的演示的链接:

    https://ng-bootstrap.github.io/stackblitzes/datepicker/i18n/stackblitz.html

    你的服务应该是这样的(在我的例子中是西班牙语)

    custom-datepicker-i18n.service.ts

    import { Injectable } from '@angular/core';
    import { NgbDatepickerI18n, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
    
    const I18N_VALUES = {
      'es': {
        weekdays: ['L', 'M', 'M', 'J', 'V', 'S', 'D'],
        months: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'],
      }
      // other languages you would support
    };
    
    // Define a service holding the language. You probably already have one if your app is i18ned. Or you could also
    // use the Angular LOCALE_ID value
    @Injectable()
    export class I18n {
      language = 'es';
    }
    
    @Injectable()
    export class CustomDatepickerI18nService extends NgbDatepickerI18n {
    
      constructor(private _i18n: I18n) {
        super();
      }
    
      getWeekdayShortName(weekday: number): string {
        return I18N_VALUES[this._i18n.language].weekdays[weekday - 1];
      }
      getMonthShortName(month: number): string {
        return I18N_VALUES[this._i18n.language].months[month - 1];
      }
      getMonthFullName(month: number): string {
        return this.getMonthShortName(month);
      }
    
      getDayAriaLabel(date: NgbDateStruct): string {
        return `${date.day}-${date.month}-${date.year}`;
      }
    }
    
    

    然后你在使用 ngbdatepicker 的组件中提供服务

    my-component.ts

    import { Component, OnInit } from '@angular/core';
    
    import { NgbDatepickerI18n } from '@ng-bootstrap/ng-bootstrap';
    import { CustomDatepickerI18nService, I18n } from 'src/app/services/ngbpicker/custom-datepicker-i18n.service';
    
    
    @Component({
      selector: 'app-mi-perfil',
      templateUrl: './mi-perfil.component.html',
      styleUrls: ['./mi-perfil.component.css'],
      providers: [I18n,{provide: NgbDatepickerI18n, useClass: CustomDatepickerI18nService}]
    })
    export class MyComponent implements OnInit {
      
      constructor() {}
    
      ngOnInit(): void {}
    
    }
    
    

    您应该只需要它来使其工作。 问候!

    【讨论】:

      【解决方案2】:

      你必须做两件事

      1. DateI18nFormater 扩展了 NgbDatepickerI18n
      2. 一个 DateParserFormatter 扩展了 NgbDateParserFormatter

      在你的 module.ts 中

      providers: [
              { provide: NgbDateParserFormatter, useClass: DateParserFormatter },
              I18n, 
              { provide: NgbDatepickerI18n, useClass: DateI18nFormater }
          ]
      

      【讨论】:

      • 什么是DateParserFormatter?它来自一个模块吗?是自制的吗?我找不到它。与DateI18nFormater 相同。
      【解决方案3】:

      我使用相同的 HTML 语法

      <div class="q-datepicker position-relative w-100">
                  <input
                      formControlName="From"
                      class=" form-control rounded-corner bg-white primary-font-color"
                      placement="bottom"
                      placeholder=""
                      [minDate]=""
                      ngbDatepicker
                      #d="ngbDatepicker"
                      readonly=""
                  />
                  <button
                      type="button"
                      (click)="d.toggle()"
                  >
                      <i class="far fa-calendar-alt"></i>
                  </button>
              </div>
      

      然后我创建一个新的服务文件,包括以下内容:

      const I18N_VALUES = {
      'Eng': {weekdays: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
      months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 
      'Nov', 'Dec'],
      },
      'Frn': {
       weekdays: ['Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa', 'Di'],
       months: ['Jan', 'Fév', 'Mar', 'Avr', 'Mai', 'Juin', 'Juil', 'Aou', 'Sep','Oct','Nov', 'Déc']
       }  };
      
      
      
      
       @Injectable()
      language: string = 'Eng';
      }
      

      在那之后并在服务内部创建新类,该类在构造函数内部使用super() 扩展NgbDatepickerI18n

      并执行以下操作:

      getWeekdayShortName(weekday: number): string {
      return I18N_VALUES[this._i18n.language].weekdays[weekday - 1];}
      
      getMonthShortName(month: number): string {
      return I18N_VALUES[this._i18n.language].months[month - 1];}
      
      getMonthFullName(month: number): string {
      return this.getMonthShortName(month);}
      
       getDayAriaLabel(date: NgbDateStruct): string {
      return `${date.day}-${date.month}-${date.year}`;}
      

      在组件内部最后导入最后一个服务并在组件中添加以下内容

      providers: [{provide: NgbDatepickerI18n, useClass: nameOfYourClassWhichExtendsNgbDatepickerI18n}]
      

      【讨论】:

      • 能否请您分享该服务的完整代码,包括其名称?
      猜你喜欢
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 2016-10-19
      相关资源
      最近更新 更多