Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

146 строки
5.7 KiB

  1. import {useContext, useRef, useState} from 'react';
  2. import ListItem from '@mui/material/ListItem';
  3. // material-ui
  4. import { useTheme } from '@mui/material/styles';
  5. import {
  6. Box,
  7. ClickAwayListener,
  8. IconButton,
  9. List,
  10. ListItemButton,
  11. ListItemText,
  12. Paper,
  13. Popper,
  14. useMediaQuery
  15. } from '@mui/material';
  16. import Transitions from 'components/@extended/Transitions';
  17. import LanguageIcon from '@mui/icons-material/Language';
  18. import {FormattedMessage} from "react-intl";
  19. import * as React from "react";
  20. import LocaleContext from "components/I18nProvider";
  21. // ==============================|| HEADER CONTENT - NOTIFICATION ||============================== //
  22. const LocaleSelector = () => {
  23. const theme = useTheme();
  24. const matchesXs = useMediaQuery(theme.breakpoints.down('md'));
  25. const { setLocale } = useContext(LocaleContext);
  26. const anchorRef = useRef(null);
  27. const [open, setOpen] = useState(false);
  28. const handleToggle = () => {
  29. setOpen((prevOpen) => !prevOpen);
  30. };
  31. const handleClose = (event) => {
  32. if (anchorRef.current && anchorRef.current.contains(event.target)) {
  33. return;
  34. }
  35. setOpen(false);
  36. };
  37. const iconBackColorOpen = 'grey.300';
  38. const iconBackColor = '#ffffff';
  39. return (
  40. <Box sx={{ flexShrink: 0, ml: 0.75 }}>
  41. <IconButton
  42. disableRipple
  43. color="secondary"
  44. sx={{ color: 'text.primary', bgcolor: open ? iconBackColorOpen : iconBackColor }}
  45. aria-label="open profile"
  46. ref={anchorRef}
  47. aria-controls={open ? 'profile-grow' : undefined}
  48. aria-haspopup="true"
  49. onClick={handleToggle}
  50. >
  51. <LanguageIcon />
  52. </IconButton>
  53. <Popper
  54. placement={matchesXs ? 'bottom' : 'bottom-end'}
  55. open={open}
  56. anchorEl={anchorRef.current}
  57. role={undefined}
  58. transition
  59. disablePortal
  60. popperOptions={{
  61. modifiers: [
  62. {
  63. name: 'offset',
  64. options: {
  65. offset: [matchesXs ? -5 : 0, 9]
  66. }
  67. }
  68. ]
  69. }}
  70. >
  71. {({ TransitionProps }) => (
  72. <Transitions type="fade" in={open} {...TransitionProps}>
  73. <Paper
  74. sx={{
  75. boxShadow: theme.customShadows.z1,
  76. width: '100%',
  77. minWidth: 285,
  78. maxWidth: 420,
  79. [theme.breakpoints.down('md')]: {
  80. maxWidth: 285
  81. }
  82. }}
  83. >
  84. <ClickAwayListener onClickAway={handleClose}>
  85. <List
  86. component="nav"
  87. >
  88. <ListItem disablePadding>
  89. <ListItemButton
  90. onClick={() => {
  91. setLocale("en")
  92. localStorage.setItem('locale','en');
  93. setOpen(false);
  94. }}
  95. >
  96. <ListItemText
  97. primary= <FormattedMessage id="en"/>
  98. />
  99. </ListItemButton>
  100. </ListItem>
  101. <ListItem disablePadding>
  102. <ListItemButton
  103. onClick={() => {
  104. setLocale("zh-HK");
  105. localStorage.setItem('locale','zh-HK');
  106. setOpen(false);
  107. }}
  108. >
  109. <ListItemText
  110. primary= <FormattedMessage id="zh-HK"/>
  111. />
  112. </ListItemButton>
  113. </ListItem>
  114. <ListItem disablePadding>
  115. <ListItemButton
  116. onClick={() => {
  117. setLocale("zh-CN")
  118. localStorage.setItem('locale','zh-CN');
  119. setOpen(false);
  120. }}
  121. >
  122. <ListItemText
  123. primary= <FormattedMessage id="zh-CN"/>
  124. />
  125. </ListItemButton>
  126. </ListItem>
  127. </List>
  128. </ClickAwayListener>
  129. </Paper>
  130. </Transitions>
  131. )}
  132. </Popper>
  133. </Box>
  134. );
  135. };
  136. export default LocaleSelector;