-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[pickers] Add l10n support #4517
Changes from 8 commits
aa38ce9
db0418b
5e3bef1
d25eca1
cc9089d
5693329
25dfbaf
9f5233a
c47cce2
ba0953a
a504b74
2ae5b0c
4a07623
3f5ba41
36aa82d
bf23f6b
1c54505
b99e9e4
b9e73fa
b9eb4cb
e95d233
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as React from 'react'; | ||
import frLocale from 'date-fns/locale/fr'; | ||
import ruLocale from 'date-fns/locale/ru'; | ||
import deLocale from 'date-fns/locale/de'; | ||
import enLocale from 'date-fns/locale/en-US'; | ||
import ToggleButton from '@mui/material/ToggleButton'; | ||
import ToggleButtonGroup from '@mui/material/ToggleButtonGroup'; | ||
import TextField from '@mui/material/TextField'; | ||
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'; | ||
import { DatePicker } from '@mui/x-date-pickers/DatePicker'; | ||
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; | ||
|
||
const localeMap = { | ||
en: enLocale, | ||
fr: frLocale, | ||
ru: ruLocale, | ||
de: deLocale, | ||
}; | ||
|
||
const maskMap = { | ||
fr: '__/__/____', | ||
en: '__/__/____', | ||
ru: '__.__.____', | ||
de: '__.__.____', | ||
}; | ||
|
||
export default function LocalizedDatePicker() { | ||
const [locale, setLocale] = React.useState('ru'); | ||
const [value, setValue] = React.useState(new Date()); | ||
|
||
const selectLocale = (newLocale) => { | ||
setLocale(newLocale); | ||
}; | ||
|
||
return ( | ||
<LocalizationProvider dateAdapter={AdapterDateFns} locale={localeMap[locale]}> | ||
<div> | ||
<ToggleButtonGroup value={locale} exclusive sx={{ mb: 2, display: 'block' }}> | ||
{Object.keys(localeMap).map((localeItem) => ( | ||
<ToggleButton | ||
key={localeItem} | ||
value={localeItem} | ||
onClick={() => selectLocale(localeItem)} | ||
> | ||
{localeItem} | ||
</ToggleButton> | ||
))} | ||
</ToggleButtonGroup> | ||
<DatePicker | ||
mask={maskMap[locale]} | ||
value={value} | ||
onChange={(newValue) => setValue(newValue)} | ||
renderInput={(params) => <TextField {...params} />} | ||
/> | ||
</div> | ||
</LocalizationProvider> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as React from 'react'; | ||
import frLocale from 'date-fns/locale/fr'; | ||
import ruLocale from 'date-fns/locale/ru'; | ||
import deLocale from 'date-fns/locale/de'; | ||
import enLocale from 'date-fns/locale/en-US'; | ||
import ToggleButton from '@mui/material/ToggleButton'; | ||
import ToggleButtonGroup from '@mui/material/ToggleButtonGroup'; | ||
import TextField from '@mui/material/TextField'; | ||
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'; | ||
import { DatePicker } from '@mui/x-date-pickers/DatePicker'; | ||
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; | ||
|
||
const localeMap = { | ||
en: enLocale, | ||
fr: frLocale, | ||
ru: ruLocale, | ||
de: deLocale, | ||
}; | ||
|
||
const maskMap = { | ||
fr: '__/__/____', | ||
en: '__/__/____', | ||
ru: '__.__.____', | ||
de: '__.__.____', | ||
}; | ||
|
||
export default function LocalizedDatePicker() { | ||
const [locale, setLocale] = React.useState<keyof typeof maskMap>('ru'); | ||
const [value, setValue] = React.useState<Date | null>(new Date()); | ||
|
||
const selectLocale = (newLocale: any) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can probably type it correctly Create a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a copy past from other pages so I created a dedicated issue #4915 |
||
setLocale(newLocale); | ||
}; | ||
|
||
return ( | ||
<LocalizationProvider dateAdapter={AdapterDateFns} locale={localeMap[locale]}> | ||
flaviendelangle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<div> | ||
<ToggleButtonGroup value={locale} exclusive sx={{ mb: 2, display: 'block' }}> | ||
{Object.keys(localeMap).map((localeItem) => ( | ||
<ToggleButton | ||
key={localeItem} | ||
value={localeItem} | ||
onClick={() => selectLocale(localeItem)} | ||
> | ||
{localeItem} | ||
</ToggleButton> | ||
))} | ||
</ToggleButtonGroup> | ||
<DatePicker | ||
mask={maskMap[locale]} | ||
value={value} | ||
onChange={(newValue) => setValue(newValue)} | ||
renderInput={(params) => <TextField {...params} />} | ||
/> | ||
</div> | ||
</LocalizationProvider> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,105 @@ | ||||||
--- | ||||||
title: Pickers - Localization | ||||||
--- | ||||||
|
||||||
# Pickers - Localization | ||||||
|
||||||
<p class="description">Pickers allow to support users from different locales, with formatting, RTL, and localized strings.</p> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The official naming of the project is "Date and time pickers" Same for the title I think On the Getting Start page we have "Date / Time pickers" which is not great I think (https://mui.com/x/react-date-pickers/getting-started/) @samuelsycamore I you have a preference |
||||||
|
||||||
The default locale of MUI is English (United States). If you want to use other locales, follow the instructions below. | ||||||
|
||||||
## Localization | ||||||
alexfauquette marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Localization can impact pickers components rendering in two distincts ways: The date format, and the components attributes such as `aria-label`. | ||||||
|
||||||
### Date-engine locale | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a fan a |
||||||
|
||||||
Use `LocalizationProvider` to change the date-engine locale that is used to render pickers. Here is an example of changing the locale for the `date-fns` adapter: | ||||||
|
||||||
{{"demo": "LocalizedDatePicker.js"}} | ||||||
|
||||||
### Translation keys | ||||||
|
||||||
As the rest of MUI components, you can modify text and translations. | ||||||
alexfauquette marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
You can find all the translation keys supported in [the source](https://github.com/mui/mui-x/blob/HEAD/packages/grid/x-date-pickers/src/locales/utils/pickersLocaleTextApi.ts) | ||||||
in the GitHub repository. | ||||||
|
||||||
You can set the locale text by using the theme provider. | ||||||
|
||||||
```jsx | ||||||
import { createTheme, ThemeProvider } from '@mui/material/styles'; | ||||||
import { CalendarPicker, LocalizationProvider, bgBG } from '@mui/x-date-pickers'; | ||||||
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'; | ||||||
|
||||||
const theme = createTheme( | ||||||
{ | ||||||
palette: { | ||||||
primary: { main: '#1976d2' }, | ||||||
}, | ||||||
}, | ||||||
bgBG, | ||||||
); | ||||||
|
||||||
<ThemeProvider theme={theme}> | ||||||
<LocalizationProvider dateAdapter={AdapterDateFns}> | ||||||
<CalendarPicker /> | ||||||
</LocalizationProvider> | ||||||
</ThemeProvider>; | ||||||
``` | ||||||
|
||||||
Note that `createTheme` accepts any number of arguments. | ||||||
If you are already using the [translations of the core components](/material-ui/guides/localization/#locale-text) or the [translations of the data grid](/x/react-data-grid/localization/#locale-text), you can add `bgBG` as a new argument. | ||||||
The same import works for `DataGridPro` as it's an extension of `DataGrid`. | ||||||
|
||||||
```jsx | ||||||
import { createTheme, ThemeProvider } from '@mui/material/styles'; | ||||||
import { DataGrid, bgBG as dataGridBgBG } from '@mui/x-data-grid'; | ||||||
import { bgBG as coreBgBG } from '@mui/material/locale'; | ||||||
|
||||||
import { CalendarPicker, LocalizationProvider, bgBG } from '@mui/x-date-pickers'; | ||||||
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'; | ||||||
|
||||||
const theme = createTheme( | ||||||
{ | ||||||
palette: { | ||||||
primary: { main: '#1976d2' }, | ||||||
}, | ||||||
}, | ||||||
bgBG, // x-date-pickers translations | ||||||
dataGridBgBG, // x-data-grid translations | ||||||
coreBgBG, // core translations | ||||||
alexfauquette marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
); | ||||||
|
||||||
<ThemeProvider theme={theme}> | ||||||
<LocalizationProvider dateAdapter={AdapterDateFns}> | ||||||
<CalendarPicker /> | ||||||
<DataGrid /> | ||||||
</LocalizationProvider> | ||||||
</ThemeProvider>; | ||||||
``` | ||||||
|
||||||
If you want to pass language translations without using `createTheme` and `ThemeProvider`, you can directly load the language translations from the `@mui/x-date-pickers` or `@mui/x-date-pickers-pro` package and pass them to the `LocalizationProvider`. | ||||||
|
||||||
```jsx | ||||||
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'; | ||||||
import { CalendarPicker, LocalizationProvider, bgBG } from '@mui/x-date-pickers'; | ||||||
|
||||||
<LocalizationProvider | ||||||
dateAdapter={AdapterDateFns} | ||||||
localeText={bgBG.components.MuiLocalizationProvider.defaultProps.localeText} | ||||||
> | ||||||
<CalendarPicker /> | ||||||
</LocalizationProvider>; | ||||||
``` | ||||||
|
||||||
### Supported locales | ||||||
|
||||||
| Locale | BCP 47 language tag | Import name | | ||||||
| :---------------------- | :------------------ | :---------- | | ||||||
| English (United States) | en-US | `enUS` | | ||||||
| French | fr-FR | `frFR` | | ||||||
|
||||||
You can [find the source](https://github.com/mui/mui-x/tree/HEAD/packages/grid/x-date-pickers/src/locales) in the GitHub repository. | ||||||
|
||||||
To create your own translation or to customize the English text, copy this file to your project, make any changes needed and import the locale from there. | ||||||
Note that these translations of the Data grid component depend on the [Localization strategy](/material-ui/guides/localization/) of the whole library. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just realized this rule was not applied