-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[charts] Add a localization provider #17325
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
Open
alexfauquette
wants to merge
15
commits into
mui:master
Choose a base branch
from
alexfauquette:add-localization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+644
−17
Open
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8b21dd5
[charts] Add a localization provider
alexfauquette 4362b15
document localization
alexfauquette 1033397
scripts
alexfauquette 8f05a9e
typescript:formating
alexfauquette 896a175
fix test
alexfauquette e825b1f
feedback
alexfauquette 9c3b2f3
fix link
alexfauquette f0ef6bd
script add .
alexfauquette 5b76242
update docs
alexfauquette 7a2dfa1
feedback
alexfauquette 04c984b
script
alexfauquette 5f5392e
Merge remote-tracking branch 'upstream/master' into add-localization
alexfauquette f59b08b
fix proptypes
alexfauquette 5dcdb10
export localization from pro package
alexfauquette 30b2819
Merge remote-tracking branch 'upstream/master' into add-localization
alexfauquette File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
docs/data/charts/localization/ChartsLocalisationTableNoSnap.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as React from 'react'; | ||
import LocalisationTable from 'docsx/src/modules/components/LocalizationTable'; | ||
import data from './data.json'; | ||
|
||
export default function ChartsLocalisationTableNoSnap() { | ||
return <LocalisationTable data={data} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as React from 'react'; | ||
import { BarChart } from '@mui/x-charts/BarChart'; | ||
|
||
export default function CustomLocaleOverlay() { | ||
return ( | ||
<BarChart | ||
loading | ||
localeText={{ loading: 'Data are coming 🧙♂️' }} | ||
series={[]} | ||
height={200} | ||
width={300} | ||
xAxis={[{ scaleType: 'band', data: ['Q1', 'Q2', 'Q3', 'Q4'] }]} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as React from 'react'; | ||
import { BarChart } from '@mui/x-charts/BarChart'; | ||
|
||
export default function CustomLocaleOverlay() { | ||
return ( | ||
<BarChart | ||
loading | ||
localeText={{ loading: 'Data are coming 🧙♂️' }} | ||
series={[]} | ||
height={200} | ||
width={300} | ||
xAxis={[{ scaleType: 'band', data: ['Q1', 'Q2', 'Q3', 'Q4'] }]} | ||
/> | ||
); | ||
} |
8 changes: 8 additions & 0 deletions
8
docs/data/charts/localization/CustomLocaleOverlay.tsx.preview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<BarChart | ||
loading | ||
localeText={{ loading: 'Data are coming 🧙♂️' }} | ||
series={[]} | ||
height={200} | ||
width={300} | ||
xAxis={[{ scaleType: 'band', data: ['Q1', 'Q2', 'Q3', 'Q4'] }]} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"languageTag": "fr-FR", | ||
"importName": "frFR", | ||
"localeName": "French", | ||
"missingKeysCount": 0, | ||
"totalKeysCount": 2, | ||
"githubLink": "https://github.com/mui/mui-x/blob/master/packages/x-charts/src/locales/frFR.ts" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
--- | ||
title: Charts - Localization | ||
productId: x-charts | ||
components: ChartsLocalizationProvider, ChartsDataContainer, ChartsLocalizationProvider | ||
--- | ||
|
||
# Charts - Localization | ||
|
||
<p class="description">The Charts allows to support users from different locales, with formatting, and localized strings.</p> | ||
|
||
The default locale of MUI X is English (United States). If you want to use other locales, follow the instructions below. | ||
|
||
## Translation keys | ||
|
||
You can use the `localeText` prop to pass in your own text and translations. | ||
You can find all the translation keys supported in [the source](https://github.com/mui/mui-x/blob/-/packages/x-charts/src/constants/defaultLocale.ts) | ||
in the GitHub repository. | ||
In the following example, the labels of the loading overlay are customized. | ||
|
||
{{"demo": "CustomLocaleOverlay.js", "bg": "inline"}} | ||
|
||
## Locale text | ||
|
||
The default locale of MUI X is English (United States). | ||
alexfauquette marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
You can use the theme to configure the locale text: | ||
|
||
```jsx | ||
import { createTheme, ThemeProvider } from '@mui/material/styles'; | ||
import { BarChart } from '@mui/x-charts/BarChart'; | ||
import { frFR } from '@mui/x-charts/locales'; | ||
// Or import { frFR } from '@mui/x-charts-pro/locales'; | ||
|
||
const theme = createTheme( | ||
{ | ||
palette: { | ||
primary: { main: '#1976d2' }, | ||
}, | ||
}, | ||
frFR, | ||
); | ||
|
||
<ThemeProvider theme={theme}> | ||
<BarChart /> | ||
</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), you can add `frFR` as a new argument. | ||
The same import works for Charts Pro as it's an extension of Charts. | ||
|
||
```jsx | ||
import { createTheme, ThemeProvider } from '@mui/material/styles'; | ||
import { BarChart } from '@mui/x-charts/BarChart'; | ||
import { frFR } from '@mui/x-charts/locales'; | ||
import { frFR as dataGridFrFR } from '@mui/x-date-pickers/locales'; | ||
import { frFR as pickersFrFR } from '@mui/x-date-pickers/locales'; | ||
import { frFR as coreFrFR } from '@mui/material/locale'; | ||
|
||
const theme = createTheme( | ||
{ | ||
palette: { | ||
primary: { main: '#1976d2' }, | ||
}, | ||
}, | ||
frFR, // x-charts translations | ||
dataGridFrFR, // x-data-grid translations | ||
pickersFrFR, // x-date-pickers translations | ||
coreFrFR, // core translations | ||
); | ||
|
||
<ThemeProvider theme={theme}> | ||
<BarChart /> | ||
</ThemeProvider>; | ||
``` | ||
|
||
If you want to pass language translations directly to the Data Grid without using `createTheme()` and `ThemeProvider`, you can directly load the language translations from `@mui/x-data-grid/locales`. | ||
|
||
```jsx | ||
import { BarChart } from '@mui/x-charts'; | ||
import { nlNL } from '@mui/x-charts/locales'; | ||
|
||
<BarChart | ||
localeText={nlNL.components.MuiChartsLocalizationProvider.defaultProps.localeText} | ||
/>; | ||
``` | ||
|
||
### Using ChartsLocalizationProvider | ||
|
||
If you want to pass language translations without using `createTheme()` and `ThemeProvider`, | ||
you can directly load the language translations from the `@mui/x-charts` or `@mui/x-charts-pro` package and pass them to the `ChartsLocalizationProvider`. | ||
|
||
```jsx | ||
import { ChartsLocalizationProvider } from '@mui/x-date-pickers/ChartsLocalizationProvider'; | ||
import { deDE } from '@mui/x-date-pickers/locales'; | ||
import { DatePicker } from '@mui/x-date-pickers/DatePicker'; | ||
|
||
<ChartsLocalizationProvider | ||
localeText={deDE.components.MuiChartsLocalizationProvider.defaultProps.localeText} | ||
> | ||
<DatePicker /> | ||
</ChartsLocalizationProvider>; | ||
``` | ||
|
||
### Supported locales | ||
|
||
{{"demo": "ChartsLocalisationTableNoSnap.js", "hideToolbar": true, "bg": "inline"}} | ||
|
||
You can [find the source](https://github.com/mui/mui-x/tree/HEAD/packages/x-charts/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. | ||
alexfauquette marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## RTL Support | ||
|
||
Right-to-left languages such as Arabic, Persian, or Hebrew are supported. | ||
Follow [this guide](/material-ui/customization/right-to-left/) to use them. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as React from 'react'; | ||
import ApiPage from 'docs/src/modules/components/ApiPage'; | ||
import mapApiPageTranslations from 'docs/src/modules/utils/mapApiPageTranslations'; | ||
import jsonPageContent from './charts-localization-provider.json'; | ||
|
||
export default function Page(props) { | ||
const { descriptions, pageContent } = props; | ||
return <ApiPage descriptions={descriptions} pageContent={pageContent} />; | ||
} | ||
|
||
Page.getInitialProps = () => { | ||
const req = require.context( | ||
'docsx/translations/api-docs/charts/charts-localization-provider', | ||
false, | ||
/\.\/charts-localization-provider.*.json$/, | ||
); | ||
const descriptions = mapApiPageTranslations(req); | ||
|
||
return { | ||
descriptions, | ||
pageContent: jsonPageContent, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"props": { "localeText": { "type": { "name": "object" } } }, | ||
"name": "ChartsLocalizationProvider", | ||
"imports": [ | ||
"import { ChartsLocalizationProvider } from '@mui/x-charts/ChartsLocalizationProvider';", | ||
"import { ChartsLocalizationProvider } from '@mui/x-charts';", | ||
"import { ChartsLocalizationProvider } from '@mui/x-charts-pro';" | ||
], | ||
"classes": [], | ||
"spread": true, | ||
"themeDefaultProps": null, | ||
"muiName": "MuiChartsLocalizationProvider", | ||
"filename": "/packages/x-charts/src/ChartsLocalizationProvider/ChartsLocalizationProvider.tsx", | ||
"inheritance": null, | ||
"demos": "<ul><li><a href=\"/x/react-charts/localization/\">Charts - Localization</a></li></ul>", | ||
"cssComponent": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as React from 'react'; | ||
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs'; | ||
import * as pageProps from 'docsx/data/charts/localization/localization.md?muiMarkdown'; | ||
|
||
export default function Page() { | ||
return <MarkdownDocs {...pageProps} disableAd />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should this be called
ChartsLocalizationTableNoSnap
? It seems like we're using enUS spelling