Files
Checkmate/Client/src/Components/LanguageSelector.jsx
T

140 lines
3.3 KiB
React
Raw Normal View History

2025-02-07 01:59:54 +03:00
import { useTranslation } from "react-i18next";
import { Box, MenuItem, Select, Stack } from "@mui/material";
import { useTheme } from "@emotion/react";
2025-02-09 07:52:31 -08:00
import "flag-icons/css/flag-icons.min.css";
import { useSelector, useDispatch } from "react-redux";
import { setLanguage } from "../Features/UI/uiSlice";
2025-02-07 01:59:54 +03:00
const LanguageSelector = () => {
const { i18n } = useTranslation();
const theme = useTheme();
const dispatch = useDispatch();
const language = useSelector((state) => state.ui.language);
2025-02-07 01:59:54 +03:00
const handleChange = (event) => {
const newLang = event.target.value;
dispatch(setLanguage(newLang));
2025-02-07 01:59:54 +03:00
i18n.changeLanguage(newLang);
};
// i18n instance'ından mevcut dilleri al
const languages = Object.keys(i18n.options.resources || {});
return (
<Select
value={language}
onChange={handleChange}
size="small"
sx={{
height: 28,
width: 64,
backgroundColor: theme.palette.primary.main,
color: theme.palette.primary.contrastText,
borderRadius: theme.shape.borderRadius,
fontSize: 10,
"& .MuiOutlinedInput-notchedOutline": {
borderColor: theme.palette.primary.lowContrast,
borderRadius: theme.shape.borderRadius,
},
"&:hover .MuiOutlinedInput-notchedOutline": {
borderColor: theme.palette.primary.lowContrast,
},
"&.Mui-focused .MuiOutlinedInput-notchedOutline": {
borderColor: theme.palette.primary.lowContrast,
},
"& .MuiSvgIcon-root": {
color: theme.palette.primary.contrastText,
width: 16,
height: 16,
right: 4,
top: "calc(50% - 8px)",
},
"& .MuiSelect-select": {
2025-02-07 02:03:25 +03:00
padding: "2px 20px 2px 8px",
2025-02-07 01:59:54 +03:00
display: "flex",
alignItems: "center",
fontSize: 10,
},
}}
MenuProps={{
PaperProps: {
sx: {
backgroundColor: theme.palette.primary.main,
borderRadius: theme.shape.borderRadius,
marginTop: 1,
width: 64,
"& .MuiMenuItem-root": {
2025-02-07 02:03:25 +03:00
padding: "2px 8px",
2025-02-07 01:59:54 +03:00
minHeight: 28,
fontSize: 10,
},
},
},
anchorOrigin: {
vertical: "bottom",
horizontal: "left",
},
transformOrigin: {
vertical: "top",
horizontal: "left",
},
}}
>
{languages.map((lang) => {
const flag = lang ? `fi fi-${lang}` : null;
return (
<MenuItem
key={lang}
value={lang}
sx={{
color: theme.palette.primary.contrastText,
2025-02-07 01:59:54 +03:00
"&:hover": {
backgroundColor: theme.palette.primary.lowContrast,
},
"&.Mui-selected": {
backgroundColor: theme.palette.primary.lowContrast,
"&:hover": {
backgroundColor: theme.palette.primary.lowContrast,
2025-02-07 01:59:54 +03:00
},
},
}}
>
<Stack
direction="row"
spacing={2}
alignItems="center"
ml={0.5}
2025-02-07 01:59:54 +03:00
>
<Box
component="span"
sx={{
width: 16,
height: 12,
display: "flex",
alignItems: "center",
"& img": {
width: "100%",
height: "100%",
objectFit: "cover",
borderRadius: 0.5,
},
}}
>
{flag && <span className={flag} />}
</Box>
<Box
component="span"
sx={{ textTransform: "uppercase", fontSize: 10 }}
>
{lang}
</Box>
</Stack>
</MenuItem>
);
})}
2025-02-07 01:59:54 +03:00
</Select>
);
};
export default LanguageSelector;