Files
Homarr/src/components/Settings/LanguageSwitch.tsx

111 lines
3.2 KiB
TypeScript
Raw Normal View History

import { Group, Select, Stack, Text } from '@mantine/core';
2022-08-18 21:46:46 +02:00
import { showNotification } from '@mantine/notifications';
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';
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;
const configLocale = getCookie('config-locale');
const { locale, locales, push } = useRouter();
const [selectedLanguage, setSelectedLanguage] = useState<string>(
(configLocale as string) ?? locale ?? 'en'
);
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,
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(() => {
setCookie('config-locale', value, {
maxAge: 60 * 60 * 24 * 30,
sameSite: 'strict',
});
push('/', '/', { locale: value });
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
icon={<Text>{getUnicodeFlagIcon(getLanguageByCode(selectedLanguage).shortName)}</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}
searchable
filter={(value, item) => {
const selectItems = item as unknown as { value: string, language: Language };
return (
selectItems.language.originalName.trim().includes(value.trim()) ||
selectItems.language.translatedName.trim().includes(value.trim())
);
}}
2022-08-24 17:58:14 +02:00
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>
<Text>{getUnicodeFlagIcon(language.shortName)}</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>
)
);