How to change the language for KeyboardDatePicker material ui?

坚强是说给别人听的谎言 提交于 2020-02-24 11:40:32

问题


I am currently using material ui on the web and I use the KeyboardDatePicker API and it works perfectly, but the names of the months and the text of the buttons are shown in English and I want to change them to Spanish. I read the API documentation but I can't find information about it.

Anyone know how to change the language?


回答1:


Try importing spanish moment locale and then using it in MuiPickersUtilsProvider:

import React from "react";
import ReactDOM from "react-dom";
import {
  KeyboardDatePicker,
  MuiPickersUtilsProvider
} from "@material-ui/pickers";
import MomentUtils from "@date-io/moment";
import "moment/locale/es";

import "./styles.css";

function App() {
  return (
    <div className="App">
      <MuiPickersUtilsProvider locale="es" utils={MomentUtils}>
        <KeyboardDatePicker />
      </MuiPickersUtilsProvider>
    </div>
  );
}



回答2:


Another solution which worked for me is using date-fns/locale/*. The documentation can be found here.

For example in german:

import {KeyboardDatePicker, MuiPickersUtilsProvider} from "@material-ui/pickers";
import DateFnsUtils from "@date-io/date-fns";
import deLocale from "date-fns/locale/de";


render () {
   return 
   (
      <MuiPickersUtilsProvider utils={DateFnsUtils} locale={deLocale}>
      .
      .
      .
      </MuiPickersUtilsProvider>
   )
}

Result:




回答3:


For spanish the solution that worked for me is the following

import 'date-fns';
import React from 'react';
import { MuiPickersUtilsProvider, KeyboardDatePicker } from '@material-ui/pickers';
import FormControl from '@material-ui/core/FormControl';
import DateFnsUtils from '@date-io/date-fns';
import deLocale from "date-fns/locale/es";
import {useStyles} from './style';

export default function FormControlDate(props) {
    const classes = useStyles();
    return (
        <FormControl className={classes.formControl} >
            <MuiPickersUtilsProvider locale={deLocale} utils={DateFnsUtils} >
                <KeyboardDatePicker
                                    disableToolbar
                                    variant="inline"
                                    format="dd/MM/yyyy"
                                    margin="normal"
                                    id={props.name}
                                    name={props.name}
                                    key={props.name}
                                    label={props.label}
                                    value={props.value}
                                    onChange={date=>{
                                        props.handleChange({"target":{"name":props.name,"value":date}})
                                    }}
                                    KeyboardButtonProps={{
                                        'aria-label': 'change date',
                                    }}
                />
            </MuiPickersUtilsProvider>
        </FormControl>  
    )
}


来源:https://stackoverflow.com/questions/58677350/how-to-change-the-language-for-keyboarddatepicker-material-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!