2022-08-25 12:28:43 +02:00
|
|
|
import { Group, Select, Stack, Text } from '@mantine/core';
|
2022-08-18 21:46:46 +02:00
|
|
|
import { showNotification } from '@mantine/notifications';
|
2022-08-25 12:28:43 +02:00
|
|
|
import getUnicodeFlagIcon from 'country-flag-icons/unicode';
|
2022-08-18 21:46:46 +02:00
|
|
|
|
|
|
|
|
import { forwardRef, useState } from 'react';
|
2022-08-22 09:50:54 +02:00
|
|
|
import { useTranslation } from 'next-i18next';
|
2022-08-24 17:58:14 +02:00
|
|
|
import { useRouter } from 'next/router';
|
2022-08-25 11:07:25 +02:00
|
|
|
import { getCookie, setCookie } from 'cookies-next';
|
2022-08-24 17:58:14 +02:00
|
|
|
import { getLanguageByCode, Language } from '../../languages/language';
|
2022-08-18 21:46:46 +02:00
|
|
|
|
|
|
|
|
export default function LanguageSwitch() {
|
2022-08-22 09:50:54 +02:00
|
|
|
const { t, i18n } = useTranslation('settings/general/internationalization');
|
2022-08-24 17:58:14 +02:00
|
|
|
const { changeLanguage } = i18n;
|
2022-08-25 11:07:25 +02:00
|
|
|
const configLocale = getCookie('config-locale');
|
2022-08-24 17:58:14 +02:00
|
|
|
const { locale, locales } = useRouter();
|
2022-08-25 11:07:25 +02:00
|
|
|
const [selectedLanguage, setSelectedLanguage] = useState<string | undefined>(
|
|
|
|
|
(configLocale as string) ?? locale
|
|
|
|
|
);
|
2022-08-18 21:46:46 +02:00
|
|
|
|
2022-08-24 17:58:14 +02:00
|
|
|
const data = locales
|
|
|
|
|
? locales.map((localeItem) => ({
|
|
|
|
|
value: localeItem,
|
|
|
|
|
label: getLanguageByCode(localeItem).originalName,
|
2022-08-25 12:28:43 +02:00
|
|
|
icon: getUnicodeFlagIcon(localeItem),
|
2022-08-24 17:58:14 +02:00
|
|
|
language: getLanguageByCode(localeItem),
|
|
|
|
|
}))
|
|
|
|
|
: [];
|
2022-08-18 21:46:46 +02:00
|
|
|
|
|
|
|
|
const onChangeSelect = (value: string) => {
|
2022-08-24 17:58:14 +02:00
|
|
|
setSelectedLanguage(value);
|
2022-08-18 21:46:46 +02:00
|
|
|
|
2022-08-24 17:58:14 +02:00
|
|
|
const newLanguage = getLanguageByCode(value);
|
|
|
|
|
changeLanguage(value)
|
2022-08-18 21:46:46 +02:00
|
|
|
.then(() => {
|
2022-08-25 11:07:25 +02:00
|
|
|
setCookie('config-locale', value, {
|
|
|
|
|
maxAge: 60 * 60 * 24 * 30,
|
|
|
|
|
sameSite: 'strict',
|
|
|
|
|
});
|
|
|
|
|
|
2022-08-18 21:46:46 +02:00
|
|
|
showNotification({
|
|
|
|
|
title: 'Language changed',
|
2022-08-24 17:58:14 +02:00
|
|
|
message: `You changed the language to '${newLanguage.originalName}'`,
|
2022-08-18 21:46:46 +02:00
|
|
|
color: 'green',
|
|
|
|
|
autoClose: 5000,
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
showNotification({
|
|
|
|
|
title: 'Failed to change language',
|
2022-08-24 17:58:14 +02:00
|
|
|
message: `Failed to change to '${newLanguage.originalName}', Error:'${err}`,
|
2022-08-18 21:46:46 +02:00
|
|
|
color: 'red',
|
|
|
|
|
autoClose: 5000,
|
|
|
|
|
});
|
2022-08-24 17:58:14 +02:00
|
|
|
});
|
2022-08-18 21:46:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Stack>
|
|
|
|
|
<Select
|
2022-08-25 12:28:43 +02:00
|
|
|
icon={<Text>{getUnicodeFlagIcon(selectedLanguage as string)}</Text>}
|
2022-08-22 09:50:54 +02:00
|
|
|
label={t('label')}
|
2022-08-24 17:58:14 +02:00
|
|
|
data={data}
|
2022-08-18 21:46:46 +02:00
|
|
|
itemComponent={SelectItem}
|
|
|
|
|
nothingFound="Nothing found"
|
|
|
|
|
onChange={onChangeSelect}
|
|
|
|
|
value={selectedLanguage}
|
2022-08-24 17:58:14 +02:00
|
|
|
defaultValue={locale}
|
|
|
|
|
styles={{
|
|
|
|
|
icon: {
|
|
|
|
|
width: 42,
|
|
|
|
|
},
|
|
|
|
|
input: {
|
|
|
|
|
paddingLeft: '45px !important',
|
|
|
|
|
},
|
|
|
|
|
}}
|
2022-08-18 21:46:46 +02:00
|
|
|
/>
|
|
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ItemProps extends React.ComponentPropsWithoutRef<'div'> {
|
|
|
|
|
image: string;
|
2022-08-24 17:58:14 +02:00
|
|
|
language: Language;
|
2022-08-18 21:46:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SelectItem = forwardRef<HTMLDivElement, ItemProps>(
|
2022-08-24 17:58:14 +02:00
|
|
|
({ language, image, ...others }: ItemProps, ref) => (
|
2022-08-18 21:46:46 +02:00
|
|
|
<div ref={ref} {...others}>
|
|
|
|
|
<Group noWrap>
|
2022-08-25 12:28:43 +02:00
|
|
|
<Text>{getUnicodeFlagIcon(language.originalName as string)}</Text>
|
2022-08-18 21:46:46 +02:00
|
|
|
|
|
|
|
|
<div>
|
2022-08-24 17:58:14 +02:00
|
|
|
<Text size="sm">
|
|
|
|
|
{language.originalName} ({language.translatedName})
|
|
|
|
|
</Text>
|
2022-08-18 21:46:46 +02:00
|
|
|
</div>
|
|
|
|
|
</Group>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
);
|