primeng p-calendar组件语言切换器
prime-ng p-calendar component language switcher
我是 angular2 的新手。
我正在尝试为 angular2 应用程序开发语言切换器。
我配置 ng2-translate 没有问题,我的语言切换器适用于标签和文本。
现在我想为 primeng 组件支持 i18n p-calendar。
如 prime-ng 文档所述:
不同语言和格式的本地化是通过将设置对象绑定到语言环境来定义的 属性。以下是西班牙日历。
<p-calendar [(ngModel)]="dateValue" [locale]="es"></p-calendar>
export class MyModel {
es: any;
ngOnInit() {
this.es = {
//date
closeText: "Cerrar",
prevText: "<Ant",
nextText: "Sig>",
currentText: "Hoy",
monthNames: [ "enero","febrero","marzo","abril","mayo","junio",
"julio","agosto","septiembre","octubre","noviembre","diciembre" ],
monthNamesShort: [ "ene","feb","mar","abr","may","jun",
"jul","ago","sep","oct","nov","dic" ],
dayNames: [ "domingo","lunes","martes","miércoles","jueves","viernes","sábado" ],
dayNamesShort: [ "dom","lun","mar","mié","jue","vie","sáb" ],
dayNamesMin: [ "D","L","M","X","J","V","S" ],
weekHeader: "Sm",
dateFormat: "dd/mm/yy",
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
yearSuffix: ""
//time
timeOnlyTitle: 'Elegir una hora',
timeText: 'Hora',
hourText: 'Horas',
minuteText: 'Minutos',
secondText: 'Segundos',
millisecText: 'Milisegundos',
microsecText: 'Microsegundos',
timezoneText: 'Uso horario',
timeFormat: 'HH:mm',
timeSuffix: '',
amNames: ['a.m.', 'AM', 'A'],
pmNames: ['p.m.', 'PM', 'P'],
};
}
}
我的问题是如何在我的应用程序的任何组件上绑定 "es" 之类的全局对象并在本地对象之间切换?
以下是我的代码片段:
app.component.ts
import {Component, ElementRef, AfterViewInit, OnInit} from '@angular/core';
import {TranslateService} from 'ng2-translate/ng2-translate';
import {environment} from '../environments/environment';
import {SelectItem} from 'primeng/primeng';
const SUPPORTED_LANGUAGES:Array<SelectItem> = [
{label: 'English', value: 'en'},
{label: 'French', value: 'fr'},
];
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, AfterViewInit {
ngOnInit():void {
this.calendar_fr = {
closeText: "Fermer",
prevText: "Précédent",
nextText: "Suivant",
currentText: "Aujourd'hui",
monthNames: [ "janvier", "février", "mars", "avril", "mai", "juin",
"juillet", "août", "septembre", "octobre", "novembre", "décembre" ],
monthNamesShort: [ "janv.", "févr.", "mars", "avr.", "mai", "juin",
"juil.", "août", "sept.", "oct.", "nov.", "déc." ],
dayNames: [ "dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi" ],
dayNamesShort: [ "dim.", "lun.", "mar.", "mer.", "jeu.", "ven.", "sam." ],
dayNamesMin: [ "D","L","M","M","J","V","S" ],
weekHeader: "Sem.",
dateFormat: "dd/mm/yy",
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
yearSuffix: ""
};
this.calendar_en = {
closeText: "Done",
prevText: "Prev",
nextText: "Next",
currentText: "Today",
monthNames: [ "January","February","March","April","May","June",
"July","August","September","October","November","December" ],
monthNamesShort: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ],
dayNames: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ],
dayNamesShort: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ],
dayNamesMin: [ "Su","Mo","Tu","We","Th","Fr","Sa" ],
weekHeader: "Wk",
dateFormat: "dd/mm/yy",
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
yearSuffix: "" };
}
public supportedLanguages:SelectItem[] = SUPPORTED_LANGUAGES;
public calendar: any;
public calendar_fr: any;
public calendar_en: any;
public lang:string;
constructor(private el:ElementRef, private translate:TranslateService) {
translate.addLangs(["en", "fr"]);
translate.setDefaultLang('en');
let browserLang = translate.getBrowserLang();
let defaultLanguage = browserLang.match(/en|fr/) ? browserLang : 'en';
translate.use(defaultLanguage);
this.calendar = defaultLanguage == 'fr' ? this.calendar_fr : this.calendar_en;
}
changeLanguage() {
this.translate.use(this.lang);
this.calendar = this.lang == 'fr' ? this.calendar_fr : this.calendar_en;
}
}
app.component.html
...
<p-dropdown [options]="supportedLanguages" [(ngModel)]="lang" [autoWidth]="false" (onChange)="changeLanguage()"></p-dropdown>
...
如何在我的所有组件上使用 locale="calendar" 的 p-calendar 并且它们会监听语言变化?
<p-calendar id="calendar" [locale]="calendar"></p-calendar>
[locale] 绑定上似乎也有一个 issue,知道如何解决这个问题吗?
您好,您可以使用这个:
<p-calendar *ngIf="calendar===calendar_fr" [locale]="calendar_fr"></p-calendar>
<p-calendar *ngIf="calendar===calendar_en" [locale]="calendar_en"></p-calendar>
PS : 这是我找到的唯一方法:).
解法:
<p-calendar [(ngModel)]="currentDate" [minDate]="minDate" [maxDate]="maxDate" [inline]="true"
[disabledDays]="[0,6]" (onSelect)="selectedDate($event)" [showOnFocus]="true" [locale]="CALENDER_CONFIG_EN" [monthNavigator]='true'
[selectOtherMonths]='true' [disabled]="noDates" [disabledDates]="disabledPreviousMonth"></p-calendar>
在组件中:
currentDate: Date;
minDate: Date;
maxDate: Date
CALENDER_CONFIG_EN = {
firstDayOfWeek: 1,
dayNames: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
dayNamesShort: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
dayNamesMin: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December'],
monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
today: 'Today',
clear: 'Clear',
}
我是 angular2 的新手。 我正在尝试为 angular2 应用程序开发语言切换器。 我配置 ng2-translate 没有问题,我的语言切换器适用于标签和文本。
现在我想为 primeng 组件支持 i18n p-calendar。
如 prime-ng 文档所述:
不同语言和格式的本地化是通过将设置对象绑定到语言环境来定义的 属性。以下是西班牙日历。
<p-calendar [(ngModel)]="dateValue" [locale]="es"></p-calendar>
export class MyModel {
es: any;
ngOnInit() {
this.es = {
//date
closeText: "Cerrar",
prevText: "<Ant",
nextText: "Sig>",
currentText: "Hoy",
monthNames: [ "enero","febrero","marzo","abril","mayo","junio",
"julio","agosto","septiembre","octubre","noviembre","diciembre" ],
monthNamesShort: [ "ene","feb","mar","abr","may","jun",
"jul","ago","sep","oct","nov","dic" ],
dayNames: [ "domingo","lunes","martes","miércoles","jueves","viernes","sábado" ],
dayNamesShort: [ "dom","lun","mar","mié","jue","vie","sáb" ],
dayNamesMin: [ "D","L","M","X","J","V","S" ],
weekHeader: "Sm",
dateFormat: "dd/mm/yy",
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
yearSuffix: ""
//time
timeOnlyTitle: 'Elegir una hora',
timeText: 'Hora',
hourText: 'Horas',
minuteText: 'Minutos',
secondText: 'Segundos',
millisecText: 'Milisegundos',
microsecText: 'Microsegundos',
timezoneText: 'Uso horario',
timeFormat: 'HH:mm',
timeSuffix: '',
amNames: ['a.m.', 'AM', 'A'],
pmNames: ['p.m.', 'PM', 'P'],
};
}
}
我的问题是如何在我的应用程序的任何组件上绑定 "es" 之类的全局对象并在本地对象之间切换? 以下是我的代码片段:
app.component.ts
import {Component, ElementRef, AfterViewInit, OnInit} from '@angular/core';
import {TranslateService} from 'ng2-translate/ng2-translate';
import {environment} from '../environments/environment';
import {SelectItem} from 'primeng/primeng';
const SUPPORTED_LANGUAGES:Array<SelectItem> = [
{label: 'English', value: 'en'},
{label: 'French', value: 'fr'},
];
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, AfterViewInit {
ngOnInit():void {
this.calendar_fr = {
closeText: "Fermer",
prevText: "Précédent",
nextText: "Suivant",
currentText: "Aujourd'hui",
monthNames: [ "janvier", "février", "mars", "avril", "mai", "juin",
"juillet", "août", "septembre", "octobre", "novembre", "décembre" ],
monthNamesShort: [ "janv.", "févr.", "mars", "avr.", "mai", "juin",
"juil.", "août", "sept.", "oct.", "nov.", "déc." ],
dayNames: [ "dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi" ],
dayNamesShort: [ "dim.", "lun.", "mar.", "mer.", "jeu.", "ven.", "sam." ],
dayNamesMin: [ "D","L","M","M","J","V","S" ],
weekHeader: "Sem.",
dateFormat: "dd/mm/yy",
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
yearSuffix: ""
};
this.calendar_en = {
closeText: "Done",
prevText: "Prev",
nextText: "Next",
currentText: "Today",
monthNames: [ "January","February","March","April","May","June",
"July","August","September","October","November","December" ],
monthNamesShort: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ],
dayNames: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ],
dayNamesShort: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ],
dayNamesMin: [ "Su","Mo","Tu","We","Th","Fr","Sa" ],
weekHeader: "Wk",
dateFormat: "dd/mm/yy",
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
yearSuffix: "" };
}
public supportedLanguages:SelectItem[] = SUPPORTED_LANGUAGES;
public calendar: any;
public calendar_fr: any;
public calendar_en: any;
public lang:string;
constructor(private el:ElementRef, private translate:TranslateService) {
translate.addLangs(["en", "fr"]);
translate.setDefaultLang('en');
let browserLang = translate.getBrowserLang();
let defaultLanguage = browserLang.match(/en|fr/) ? browserLang : 'en';
translate.use(defaultLanguage);
this.calendar = defaultLanguage == 'fr' ? this.calendar_fr : this.calendar_en;
}
changeLanguage() {
this.translate.use(this.lang);
this.calendar = this.lang == 'fr' ? this.calendar_fr : this.calendar_en;
}
}
app.component.html
...
<p-dropdown [options]="supportedLanguages" [(ngModel)]="lang" [autoWidth]="false" (onChange)="changeLanguage()"></p-dropdown>
...
如何在我的所有组件上使用 locale="calendar" 的 p-calendar 并且它们会监听语言变化?
<p-calendar id="calendar" [locale]="calendar"></p-calendar>
[locale] 绑定上似乎也有一个 issue,知道如何解决这个问题吗?
您好,您可以使用这个:
<p-calendar *ngIf="calendar===calendar_fr" [locale]="calendar_fr"></p-calendar>
<p-calendar *ngIf="calendar===calendar_en" [locale]="calendar_en"></p-calendar>
PS : 这是我找到的唯一方法:).
解法:
<p-calendar [(ngModel)]="currentDate" [minDate]="minDate" [maxDate]="maxDate" [inline]="true"
[disabledDays]="[0,6]" (onSelect)="selectedDate($event)" [showOnFocus]="true" [locale]="CALENDER_CONFIG_EN" [monthNavigator]='true'
[selectOtherMonths]='true' [disabled]="noDates" [disabledDates]="disabledPreviousMonth"></p-calendar>
在组件中:
currentDate: Date;
minDate: Date;
maxDate: Date
CALENDER_CONFIG_EN = {
firstDayOfWeek: 1,
dayNames: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
dayNamesShort: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
dayNamesMin: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December'],
monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
today: 'Today',
clear: 'Clear',
}