Javascript 将预订日历的语言环境设置为另一种语言
我正在尝试在一个小的React初学者项目中用作预订日历 到目前为止,我已经弄明白了它是如何工作的,并且运行得非常好。不幸的是,我只能用英语显示月份(对于那些我找到了通过手动设置日期名称来切换语言的日子) 有人知道如何将月份名称改成另一种语言(在我的例子中是德语)吗Javascript 将预订日历的语言环境设置为另一种语言,javascript,reactjs,locale,Javascript,Reactjs,Locale,我正在尝试在一个小的React初学者项目中用作预订日历 到目前为止,我已经弄明白了它是如何工作的,并且运行得非常好。不幸的是,我只能用英语显示月份(对于那些我找到了通过手动设置日期名称来切换语言的日子) 有人知道如何将月份名称改成另一种语言(在我的例子中是德语)吗 import React,{Component}来自'React'; 从“道具类型”导入道具类型; 从“力矩”中导入力矩; 从“./DayNames”导入DayNames; 从“./周”导入周; 导入“/BookingCalendar
import React,{Component}来自'React';
从“道具类型”导入道具类型;
从“力矩”中导入力矩;
从“./DayNames”导入DayNames;
从“./周”导入周;
导入“/BookingCalendar.scss”;
导出默认类BookingCalendar扩展组件{
建造师(道具){
超级(道具);
此.state={
previousDisabled:this.props.disableHistory,
月份:props.selected.clone(),
选定:道具。选定,
};
this.handlePrevious=this.handlePrevious.bind(this);
this.handleNext=this.handleNext.bind(this);
this.handleSelect=this.handleSelect.bind(this);
}
updatePreviousState(){
如果(此.props.disableHistory){
const previousDisabled=this.state.month.isSame(矩(),'month')&&this.state.month.isSame(矩(),'year');
this.setState({previousDisabled});
}
}
手淫的{
const{month}=this.state;
月。减去(1,'M');
this.setState({month});
this.updatePreviousState();
}
handleNext(){
const{month}=this.state;
月份。添加(1,'M');
this.setState({month});
this.updatePreviousState();
}
handleSelect(日){
如果(此.props.clickable){
this.setState({selected:day.date});
}
}
renderMonthLabel(){
返回(
{this.state.month.format('MMMM,YYYY')}
);
}
渲染周(){
常数周=[];
完成=错误;
const date=this.state.month.clone().startOf('month').add('w'-1.startOf('isoWeek');
设monthIndex=date.month();
让计数=0;
而(!完成){
周。推();
日期。添加(1,'w');
完成=计数>2&&monthIndex!==date.month();
计数+=1;
monthIndex=日期.月份();
}
返回周;
}
render(){
返回(
{this.renderMonthLabel()}
{this.renderWeeks()}
);
}
}
BookingCalendar.propTypes={
预订:PropTypes.arrayOf(PropTypes.instanceOf(Date)),
可单击:PropTypes.bool,
disableHistory:PropTypes.bool,
所选:PropTypes.instanceOf(力矩),
};
BookingCalendar.defaultProps={
预订:[],
可点击:false,
历史:错误,
选中:矩().startOf('day'),
};
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
import DayNames from './DayNames';
import Week from './Week';
import './BookingCalendar.scss';
export default class BookingCalendar extends Component {
constructor(props) {
super(props);
this.state = {
previousDisabled: this.props.disableHistory,
month: props.selected.clone(),
selected: props.selected,
};
this.handlePrevious = this.handlePrevious.bind(this);
this.handleNext = this.handleNext.bind(this);
this.handleSelect = this.handleSelect.bind(this);
}
updatePreviousState() {
if (this.props.disableHistory) {
const previousDisabled = this.state.month.isSame(moment(), 'month') && this.state.month.isSame(moment(), 'year');
this.setState({ previousDisabled });
}
}
handlePrevious() {
const { month } = this.state;
month.subtract(1, 'M');
this.setState({ month });
this.updatePreviousState();
}
handleNext() {
const { month } = this.state;
month.add(1, 'M');
this.setState({ month });
this.updatePreviousState();
}
handleSelect(day) {
if (this.props.clickable) {
this.setState({ selected: day.date });
}
}
renderMonthLabel() {
return (
<span className='month-label'>
{this.state.month.format('MMMM, YYYY')}
</span>
);
}
renderWeeks() {
const weeks = [];
let done = false;
const date = this.state.month.clone().startOf('month').add('w' - 1).startOf('isoWeek');
let monthIndex = date.month();
let count = 0;
while (!done) {
weeks.push(<Week
bookings={this.props.bookings}
clickable={this.props.clickable}
date={date.clone()}
key={date.toString()}
month={this.state.month}
selected={this.state.selected}
selectHandler={this.handleSelect}
/>);
date.add(1, 'w');
done = count > 2 && monthIndex !== date.month();
count += 1;
monthIndex = date.month();
}
return weeks;
}
render() {
return (
<div className='booking-calendar'>
<div className='header'>
<div className='header-content'>
{this.renderMonthLabel()}
<button className='icon-previous' disabled={this.state.previousDisabled} onClick={this.handlePrevious}>
<
</button>
<button className='icon-next' onClick={this.handleNext}>></button>
</div>
</div>
<DayNames />
{this.renderWeeks()}
</div>
);
}
}
BookingCalendar.propTypes = {
bookings: PropTypes.arrayOf(PropTypes.instanceOf(Date)),
clickable: PropTypes.bool,
disableHistory: PropTypes.bool,
selected: PropTypes.instanceOf(moment),
};
BookingCalendar.defaultProps = {
bookings: [],
clickable: false,
disableHistory: false,
selected: moment().startOf('day'),
};