-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathChangeLanguageModal.tsx
80 lines (71 loc) · 2.21 KB
/
ChangeLanguageModal.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// React Component displaying different applications versions and license attributions
// List of applications : Editoast, Core, Api
import { DE, ES, FR, GB, IT, JP, RU, UA } from 'country-flag-icons/react/3x2';
import { useTranslation } from 'react-i18next';
import ModalBodySNCF from 'common/BootstrapSNCF/ModalSNCF/ModalBodySNCF';
import ModalHeaderSNCF from 'common/BootstrapSNCF/ModalSNCF/ModalHeaderSNCF';
import i18n from 'i18n';
import { useModal } from './BootstrapSNCF/ModalSNCF';
const SortedLanguages = () => {
const { t } = useTranslation('home/navbar');
const { closeModal } = useModal();
const changeLanguage = (lng: string) => {
i18n.changeLanguage(lng);
closeModal();
};
const formatLanguageWithFlag = (lng: string) => {
const title = t(`language.${lng}`);
switch (lng) {
case 'de':
return <DE title={title} />;
case 'en':
return <GB title={title} />;
case 'es':
return <ES title={title} />;
case 'fr':
return <FR title={title} />;
case 'it':
return <IT title={title} />;
case 'jp':
return <JP title={title} />;
case 'ru':
return <RU title={title} />;
case 'uk':
return <UA title={title} />;
default:
return null;
}
};
const availablesLanguages = i18n.languages.map((lng) => ({
key: lng,
value: t(`language.${lng}`),
}));
availablesLanguages.sort((a, b) => a.value.localeCompare(b.value));
return (
<>
{availablesLanguages.map((lng) => (
<button
type="button"
className="btn btn-secondary btn-block language-choice-btn"
key={`language-btn-${lng.key}`}
disabled={i18n.language === lng.key}
onClick={() => changeLanguage(lng.key)}
>
{formatLanguageWithFlag(lng.key)}
{lng.value}
</button>
))}
</>
);
};
export default function ChangeLanguageModal() {
const { t } = useTranslation('home/navbar');
return (
<div className="informations">
<ModalHeaderSNCF withCloseButton>
<h1>{t('language.languageChoice')}</h1>
</ModalHeaderSNCF>
<ModalBodySNCF>{i18n.languages && <SortedLanguages />}</ModalBodySNCF>
</div>
);
}