2024-08-13 15:26:23 +08:00
|
|
|
import { useLingui } from '@lingui/react';
|
2024-08-16 19:17:57 +08:00
|
|
|
import { useMemo } from 'preact/hooks';
|
2024-08-13 15:26:23 +08:00
|
|
|
|
2024-08-14 17:16:56 +08:00
|
|
|
import { DEFAULT_LANG, LOCALES } from '../locales';
|
|
|
|
import { activateLang } from '../utils/lang';
|
2024-08-13 15:26:23 +08:00
|
|
|
import localeCode2Text from '../utils/localeCode2Text';
|
|
|
|
|
|
|
|
export default function LangSelector() {
|
|
|
|
const { i18n } = useLingui();
|
|
|
|
|
2024-08-16 19:17:57 +08:00
|
|
|
const populatedLocales = useMemo(() => {
|
|
|
|
return LOCALES.map((lang) => {
|
|
|
|
const native = localeCode2Text({ code: lang, locale: lang });
|
|
|
|
const common = localeCode2Text(lang);
|
|
|
|
const showCommon = !!common && common !== native;
|
|
|
|
return {
|
|
|
|
code: lang,
|
|
|
|
native,
|
|
|
|
common,
|
|
|
|
showCommon,
|
|
|
|
};
|
|
|
|
}).sort((a, b) => {
|
|
|
|
// If pseudo-LOCALE, always put it at the bottom
|
|
|
|
if (a.code === 'pseudo-LOCALE') return 1;
|
|
|
|
if (b.code === 'pseudo-LOCALE') return -1;
|
|
|
|
// Sort by code
|
|
|
|
if (a.code < b.code) return -1;
|
|
|
|
if (a.code > b.code) return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}, [i18n.locale]);
|
|
|
|
|
2024-08-13 15:26:23 +08:00
|
|
|
return (
|
|
|
|
<label class="lang-selector">
|
|
|
|
🌐{' '}
|
|
|
|
<select
|
2024-08-15 17:34:24 +08:00
|
|
|
class="small"
|
2024-08-13 15:26:23 +08:00
|
|
|
value={i18n.locale || DEFAULT_LANG}
|
|
|
|
onChange={(e) => {
|
|
|
|
localStorage.setItem('lang', e.target.value);
|
|
|
|
activateLang(e.target.value);
|
|
|
|
}}
|
|
|
|
>
|
2024-08-16 19:17:57 +08:00
|
|
|
{populatedLocales.map(({ code, native, common, showCommon }) => {
|
|
|
|
if (code === 'pseudo-LOCALE') {
|
2024-08-13 15:26:23 +08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<hr />
|
2024-08-16 19:17:57 +08:00
|
|
|
<option value={code} key={code}>
|
2024-08-13 15:26:23 +08:00
|
|
|
Pseudolocalization (test)
|
|
|
|
</option>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
2024-08-16 19:17:57 +08:00
|
|
|
<option value={code} key={code}>
|
|
|
|
{showCommon ? `${native} - ${common}` : native}
|
2024-08-13 15:26:23 +08:00
|
|
|
</option>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</select>
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
}
|