Ruben Valdivia Perez
Ruben Valdivia Perez

Reputation: 396

How to change the language for KeyboardDatePicker material ui?

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?

Upvotes: 5

Views: 11842

Answers (3)

Jorge Santos Neill
Jorge Santos Neill

Reputation: 1785

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>  
    )
}

Upvotes: 0

Moritz Schmidt
Moritz Schmidt

Reputation: 2813

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:

enter image description here

Upvotes: 5

Nicolas Hevia
Nicolas Hevia

Reputation: 15837

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>
  );
}

Upvotes: 4

Related Questions