phanpy/src/components/lang-selector.jsx

43 lines
1.1 KiB
React
Raw Normal View History

2024-08-13 10:26:23 +03:00
import { useLingui } from '@lingui/react';
import { activateLang, DEFAULT_LANG, LOCALES } from '../utils/lang';
import localeCode2Text from '../utils/localeCode2Text';
export default function LangSelector() {
const { i18n } = useLingui();
return (
<label class="lang-selector">
🌐{' '}
<select
value={i18n.locale || DEFAULT_LANG}
onChange={(e) => {
localStorage.setItem('lang', e.target.value);
activateLang(e.target.value);
}}
>
{LOCALES.map((lang) => {
if (lang === 'pseudo-LOCALE') {
return (
<>
<hr />
<option value={lang} key={lang}>
Pseudolocalization (test)
</option>
</>
);
}
const common = localeCode2Text(lang);
const native = localeCode2Text({ code: lang, locale: lang });
const same = common === native;
return (
<option value={lang} key={lang}>
{same ? common : `${common} (${native})`}
</option>
);
})}
</select>
</label>
);
}