2024-09-27 06:15:34 +00:00
|
|
|
import { createMemo, For, type Component, type JSX } from "solid-js";
|
|
|
|
import Scaffold from "../material/Scaffold";
|
|
|
|
import {
|
|
|
|
AppBar,
|
|
|
|
IconButton,
|
|
|
|
List,
|
|
|
|
ListItem,
|
|
|
|
ListItemButton,
|
|
|
|
ListItemSecondaryAction,
|
|
|
|
ListItemText,
|
|
|
|
ListSubheader,
|
|
|
|
Radio,
|
|
|
|
Switch,
|
|
|
|
Toolbar,
|
|
|
|
} from "@suid/material";
|
2024-10-09 15:27:20 +00:00
|
|
|
import { ArrowBack } from "@suid/icons-material";
|
2024-09-27 06:15:34 +00:00
|
|
|
import iso639_1 from "iso-639-1";
|
|
|
|
import {
|
|
|
|
autoMatchLangTag,
|
|
|
|
createTranslator,
|
|
|
|
SUPPORTED_LANGS,
|
|
|
|
} from "../platform/i18n";
|
|
|
|
import { Title } from "../material/typography";
|
|
|
|
import type { Template } from "@solid-primitives/i18n";
|
2024-10-09 15:27:20 +00:00
|
|
|
import { useStore } from "@nanostores/solid";
|
|
|
|
import { $settings } from "./stores";
|
|
|
|
import { useNavigate } from "@solidjs/router";
|
2024-09-27 06:15:34 +00:00
|
|
|
|
2024-10-09 15:27:20 +00:00
|
|
|
const ChooseLang: Component = () => {
|
|
|
|
const navigate = useNavigate()
|
2024-09-27 06:15:34 +00:00
|
|
|
const [t] = createTranslator(
|
|
|
|
() => import("./i18n/lang-names.json"),
|
|
|
|
(code) =>
|
|
|
|
import(`./i18n/${code}.json`) as Promise<{
|
|
|
|
default: Record<string, string | undefined> & {
|
|
|
|
["lang.auto"]: Template<{ detected: string }>;
|
|
|
|
};
|
|
|
|
}>,
|
|
|
|
);
|
2024-10-09 15:27:20 +00:00
|
|
|
const settings = useStore($settings)
|
|
|
|
|
|
|
|
const code = () => settings().language
|
2024-09-27 06:15:34 +00:00
|
|
|
|
|
|
|
const unsupportedLangCodes = createMemo(() => {
|
|
|
|
return iso639_1.getAllCodes().filter((x) => !["zh", "en"].includes(x));
|
|
|
|
});
|
|
|
|
|
|
|
|
const matchedLangCode = createMemo(() => autoMatchLangTag());
|
|
|
|
|
2024-10-09 15:27:20 +00:00
|
|
|
const onCodeChange = (code?: string) => {
|
|
|
|
$settings.setKey("language", code)
|
|
|
|
}
|
|
|
|
|
2024-09-27 06:15:34 +00:00
|
|
|
return (
|
|
|
|
<Scaffold
|
|
|
|
topbar={
|
|
|
|
<AppBar position="static">
|
|
|
|
<Toolbar
|
|
|
|
variant="dense"
|
|
|
|
sx={{ paddingTop: "var(--safe-area-inset-top, 0px)" }}
|
|
|
|
>
|
2024-10-09 15:27:20 +00:00
|
|
|
<IconButton color="inherit" onClick={[navigate, -1]} disableRipple>
|
|
|
|
<ArrowBack />
|
2024-09-27 06:15:34 +00:00
|
|
|
</IconButton>
|
|
|
|
<Title>{t("Choose Language")}</Title>
|
|
|
|
</Toolbar>
|
|
|
|
</AppBar>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<List
|
|
|
|
sx={{
|
|
|
|
paddingBottom: "var(--safe-area-inset-bottom, 0)",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ListItemButton
|
|
|
|
onClick={() => {
|
2024-10-09 15:27:20 +00:00
|
|
|
onCodeChange(code() ? undefined : matchedLangCode());
|
2024-09-27 06:15:34 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ListItemText>
|
|
|
|
{t("lang.auto", {
|
|
|
|
detected: t(`lang.${matchedLangCode()}`) ?? matchedLangCode(),
|
|
|
|
})}
|
|
|
|
</ListItemText>
|
|
|
|
<ListItemSecondaryAction>
|
2024-10-09 15:27:20 +00:00
|
|
|
<Switch checked={typeof code() === "undefined"} />
|
2024-09-27 06:15:34 +00:00
|
|
|
</ListItemSecondaryAction>
|
|
|
|
</ListItemButton>
|
|
|
|
<List subheader={<ListSubheader>{t("Supported")}</ListSubheader>}>
|
|
|
|
<For each={SUPPORTED_LANGS}>
|
2024-10-09 15:27:20 +00:00
|
|
|
{(c) => (
|
2024-09-27 06:15:34 +00:00
|
|
|
<ListItemButton
|
2024-10-09 15:27:20 +00:00
|
|
|
disabled={typeof code() === "undefined"}
|
|
|
|
onClick={[onCodeChange, c]}
|
2024-09-27 06:15:34 +00:00
|
|
|
>
|
2024-10-09 15:27:20 +00:00
|
|
|
<ListItemText>{t(`lang.${c}`)}</ListItemText>
|
2024-09-27 06:15:34 +00:00
|
|
|
<ListItemSecondaryAction>
|
|
|
|
<Radio
|
2024-10-09 15:27:20 +00:00
|
|
|
checked={code() === c || (code() === undefined && matchedLangCode() == c)}
|
2024-09-27 06:15:34 +00:00
|
|
|
/>
|
|
|
|
</ListItemSecondaryAction>
|
|
|
|
</ListItemButton>
|
|
|
|
)}
|
|
|
|
</For>
|
|
|
|
</List>
|
|
|
|
|
|
|
|
<List subheader={<ListSubheader>{t("Unsupported")}</ListSubheader>}>
|
|
|
|
<For each={unsupportedLangCodes()}>
|
|
|
|
{(code) => (
|
|
|
|
<ListItem>
|
|
|
|
<ListItemText>{iso639_1.getNativeName(code)}</ListItemText>
|
|
|
|
</ListItem>
|
|
|
|
)}
|
|
|
|
</For>
|
|
|
|
</List>
|
|
|
|
</List>
|
|
|
|
</Scaffold>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ChooseLang;
|