Skip to content
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

Merged
merged 21 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = {
parserOptions: { tsconfigRootDir: __dirname, project: ['./tsconfig.json'] },
},
{
files: ['docs/src/pages/components/**/*.js', 'docs/src/pages/components/**/*.tsx'],
files: ['docs/data/**/*.js', 'docs/data/**/*.tsx'],
Copy link
Member Author

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

rules: {
'filenames/match-exported': ['error'],
},
Expand Down
2 changes: 1 addition & 1 deletion docs/data/data-grid/editing/ServerSidePersistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const useFakeMutation = () => {
);
};

export default function CellEditServerSidePersistence() {
export default function ServerSidePersistence() {
const mutateRow = useFakeMutation();

const [snackbar, setSnackbar] = React.useState(null);
Expand Down
2 changes: 1 addition & 1 deletion docs/data/data-grid/editing/ServerSidePersistence.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const useFakeMutation = () => {
);
};

export default function CellEditServerSidePersistence() {
export default function ServerSidePersistence() {
const mutateRow = useFakeMutation();

const [snackbar, setSnackbar] = React.useState<Pick<
Expand Down
58 changes: 58 additions & 0 deletions docs/data/date-pickers/localization/LocalizedDatePicker.js
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>
);
}
58 changes: 58 additions & 0 deletions docs/data/date-pickers/localization/LocalizedDatePicker.tsx
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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably type it correctly

Create a LocalKey type type LocalKey = keyof typeof maskMap and use it both for the state init and here

Copy link
Member Author

Choose a reason for hiding this comment

The 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>
);
}
105 changes: 105 additions & 0 deletions docs/data/date-pickers/localization/localization.md
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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<p class="description">Pickers allow to support users from different locales, with formatting, RTL, and localized strings.</p>
<p class="description">Date and time pickers allow to support users from different locales, with formatting, RTL, and localized strings.</p>

The official naming of the project is "Date and time pickers"
I think we can shortcut with "pickers" when inside a page that already clearly scoped the topic.
But for a title / description we should probably be explicit


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan a date-engine but I don't have a better naming


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.
1 change: 1 addition & 0 deletions docs/data/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const pages = [
},
{ pathname: '/x/react-date-pickers/date-time-picker' },
{ pathname: '/x/react-date-pickers/time-picker' },
{ pathname: '/x/react-date-pickers/localization' },
{
pathname: '/x/api/date-pickers',
title: ' • API Reference',
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/x/api/date-pickers/calendar-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"disabled": { "type": { "name": "bool" } },
"disableHighlightToday": { "type": { "name": "bool" } },
"getViewSwitchingButtonText": { "type": { "name": "func" } },
"leftArrowButtonText": { "type": { "name": "string" } },
"leftArrowButtonText": { "type": { "name": "string" }, "deprecated": true },
"loading": { "type": { "name": "bool" } },
"maxDate": { "type": { "name": "any" } },
"minDate": { "type": { "name": "any" } },
Expand All @@ -32,7 +32,7 @@
"type": { "name": "func" },
"default": "() => <span data-mui-test=\"loading-progress\">...</span>"
},
"rightArrowButtonText": { "type": { "name": "string" } },
"rightArrowButtonText": { "type": { "name": "string" }, "deprecated": true },
"shouldDisableDate": { "type": { "name": "func" } },
"shouldDisableYear": { "type": { "name": "func" } },
"showDaysOutsideCurrentMonth": { "type": { "name": "bool" } },
Expand Down
12 changes: 10 additions & 2 deletions docs/pages/x/api/date-pickers/clock-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
"type": { "name": "func" },
"default": "(seconds: string) => `${seconds} seconds`"
},
"leftArrowButtonText": { "type": { "name": "string" }, "default": "'open previous view'" },
"leftArrowButtonText": {
"type": { "name": "string" },
"default": "'open previous view'",
"deprecated": true
},
"maxTime": { "type": { "name": "any" } },
"minTime": { "type": { "name": "any" } },
"minutesStep": { "type": { "name": "number" }, "default": "1" },
Expand All @@ -37,7 +41,11 @@
},
"default": "'hours'"
},
"rightArrowButtonText": { "type": { "name": "string" }, "default": "'open next view'" },
"rightArrowButtonText": {
"type": { "name": "string" },
"default": "'open next view'",
"deprecated": true
},
"shouldDisableTime": { "type": { "name": "func" } },
"view": {
"type": {
Expand Down
12 changes: 6 additions & 6 deletions docs/pages/x/api/date-pickers/date-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"default": "/\\dap/gi"
},
"allowSameDateSelection": { "type": { "name": "bool" } },
"cancelText": { "type": { "name": "node" }, "default": "'Cancel'" },
"cancelText": { "type": { "name": "node" }, "default": "'Cancel'", "deprecated": true },
"className": { "type": { "name": "string" } },
"clearable": { "type": { "name": "bool" } },
"clearText": { "type": { "name": "node" }, "default": "'Clear'" },
"clearText": { "type": { "name": "node" }, "default": "'Clear'", "deprecated": true },
"components": { "type": { "name": "object" }, "default": "{}" },
"componentsProps": { "type": { "name": "object" }, "default": "{}" },
"defaultCalendarMonth": { "type": { "name": "any" } },
Expand All @@ -37,12 +37,12 @@
"inputRef": {
"type": { "name": "union", "description": "func<br>&#124;&nbsp;{ current?: object }" }
},
"leftArrowButtonText": { "type": { "name": "string" } },
"leftArrowButtonText": { "type": { "name": "string" }, "deprecated": true },
"loading": { "type": { "name": "bool" } },
"mask": { "type": { "name": "string" } },
"maxDate": { "type": { "name": "any" } },
"minDate": { "type": { "name": "any" } },
"okText": { "type": { "name": "node" }, "default": "'OK'" },
"okText": { "type": { "name": "node" }, "default": "'OK'", "deprecated": true },
"onAccept": { "type": { "name": "func" } },
"onClose": { "type": { "name": "func" } },
"onError": { "type": { "name": "func" } },
Expand Down Expand Up @@ -74,13 +74,13 @@
"default": "() => <span data-mui-test=\"loading-progress\">...</span>"
},
"rifmFormatter": { "type": { "name": "func" } },
"rightArrowButtonText": { "type": { "name": "string" } },
"rightArrowButtonText": { "type": { "name": "string" }, "deprecated": true },
"shouldDisableDate": { "type": { "name": "func" } },
"shouldDisableYear": { "type": { "name": "func" } },
"showDaysOutsideCurrentMonth": { "type": { "name": "bool" } },
"showTodayButton": { "type": { "name": "bool" } },
"showToolbar": { "type": { "name": "bool" } },
"todayText": { "type": { "name": "node" }, "default": "'Today'" },
"todayText": { "type": { "name": "node" }, "default": "'Today'", "deprecated": true },
"ToolbarComponent": { "type": { "name": "elementType" }, "default": "DatePickerToolbar" },
"toolbarFormat": { "type": { "name": "string" } },
"toolbarPlaceholder": { "type": { "name": "node" }, "default": "'–'" },
Expand Down
12 changes: 6 additions & 6 deletions docs/pages/x/api/date-pickers/date-range-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"type": { "name": "enum", "description": "1<br>&#124;&nbsp;2<br>&#124;&nbsp;3" },
"default": "2"
},
"cancelText": { "type": { "name": "node" }, "default": "'Cancel'" },
"cancelText": { "type": { "name": "node" }, "default": "'Cancel'", "deprecated": true },
"className": { "type": { "name": "string" } },
"clearable": { "type": { "name": "bool" } },
"clearText": { "type": { "name": "node" }, "default": "'Clear'" },
"clearText": { "type": { "name": "node" }, "default": "'Clear'", "deprecated": true },
"components": { "type": { "name": "object" }, "default": "{}" },
"componentsProps": { "type": { "name": "object" }, "default": "{}" },
"defaultCalendarMonth": { "type": { "name": "any" } },
Expand Down Expand Up @@ -50,12 +50,12 @@
"inputRef": {
"type": { "name": "union", "description": "func<br>&#124;&nbsp;{ current?: object }" }
},
"leftArrowButtonText": { "type": { "name": "string" } },
"leftArrowButtonText": { "type": { "name": "string" }, "deprecated": true },
"loading": { "type": { "name": "bool" } },
"mask": { "type": { "name": "string" }, "default": "'__/__/____'" },
"maxDate": { "type": { "name": "any" }, "default": "defaultMaxDate" },
"minDate": { "type": { "name": "any" }, "default": "defaultMinDate" },
"okText": { "type": { "name": "node" }, "default": "'OK'" },
"okText": { "type": { "name": "node" }, "default": "'OK'", "deprecated": true },
"onAccept": { "type": { "name": "func" } },
"onClose": { "type": { "name": "func" } },
"onError": { "type": { "name": "func" } },
Expand All @@ -80,14 +80,14 @@
"default": "() => <span data-mui-test=\"loading-progress\">...</span>"
},
"rifmFormatter": { "type": { "name": "func" } },
"rightArrowButtonText": { "type": { "name": "string" } },
"rightArrowButtonText": { "type": { "name": "string" }, "deprecated": true },
"shouldDisableDate": { "type": { "name": "func" } },
"shouldDisableYear": { "type": { "name": "func" } },
"showDaysOutsideCurrentMonth": { "type": { "name": "bool" } },
"showTodayButton": { "type": { "name": "bool" } },
"showToolbar": { "type": { "name": "bool" } },
"startText": { "type": { "name": "node" }, "default": "'Start'" },
"todayText": { "type": { "name": "node" }, "default": "'Today'" },
"todayText": { "type": { "name": "node" }, "default": "'Today'", "deprecated": true },
"ToolbarComponent": { "type": { "name": "elementType" } },
"toolbarFormat": { "type": { "name": "string" } },
"toolbarPlaceholder": { "type": { "name": "node" }, "default": "'–'" },
Expand Down
Loading