Home: add translation

This commit is contained in:
thislight 2025-01-02 21:05:05 +08:00
parent af9f111b27
commit 2e13fcacb5
No known key found for this signature in database
GPG key ID: FCFE5192241CCD4E
4 changed files with 66 additions and 18 deletions

View file

@ -1,27 +1,23 @@
import { import {
createSignal, createSignal,
Show, Show,
onMount,
type ParentComponent, type ParentComponent,
createRenderEffect,
createEffect, createEffect,
useTransition,
} from "solid-js"; } from "solid-js";
import { useDocumentTitle } from "../utils"; import { useDocumentTitle } from "../utils";
import Scaffold from "~material/Scaffold"; import Scaffold from "~material/Scaffold";
import { import {
AppBar,
ListItemSecondaryAction, ListItemSecondaryAction,
ListItemText, ListItemText,
MenuItem, MenuItem,
Switch, Switch,
Toolbar,
} from "@suid/material"; } from "@suid/material";
import { css } from "solid-styled"; import { css } from "solid-styled";
import { TimeSourceProvider, createTimeSource } from "~platform/timesrc"; import { TimeSourceProvider, createTimeSource } from "~platform/timesrc";
import ProfileMenuButton from "./ProfileMenuButton"; import ProfileMenuButton from "./ProfileMenuButton";
import Tabs from "~material/Tabs"; import Tabs from "~material/Tabs";
import Tab from "~material/Tab"; import Tab from "~material/Tab";
import { makeEventListener } from "@solid-primitives/event-listener";
import { $settings } from "../settings/stores"; import { $settings } from "../settings/stores";
import { useStore } from "@nanostores/solid"; import { useStore } from "@nanostores/solid";
import TrendTimelinePanel from "./TrendTimelinePanel"; import TrendTimelinePanel from "./TrendTimelinePanel";
@ -32,10 +28,20 @@ import {
default as ItemSelectionProvider, default as ItemSelectionProvider,
} from "./toots/ItemSelectionProvider"; } from "./toots/ItemSelectionProvider";
import AppTopBar from "~material/AppTopBar"; import AppTopBar from "~material/AppTopBar";
import { createTranslator } from "~platform/i18n";
import { useWindowSize } from "@solid-primitives/resize-observer";
type StringRes = Record<
"tabs.home" | "tabs.trending" | "tabs.public" | "set.prefetch-toots",
string
>;
const Home: ParentComponent = (props) => { const Home: ParentComponent = (props) => {
let panelList: HTMLDivElement; let panelList: HTMLDivElement;
useDocumentTitle("Timelines"); useDocumentTitle("Timelines");
const [t] = createTranslator(
(code) => import(`./i18n/${code}.json`) as Promise<{ default: StringRes }>,
);
const now = createTimeSource(); const now = createTimeSource();
const [, selectionState] = createSingluarItemSelection( const [, selectionState] = createSingluarItemSelection(
undefined as string | undefined, undefined as string | undefined,
@ -98,10 +104,19 @@ const Home: ParentComponent = (props) => {
} }
}; };
createEffect(() => { const windowSize = useWindowSize();
makeEventListener(window, "resize", requestRecalculateTabIndicator); createEffect((last) => {
if (last !== windowSize.width) {
requestRecalculateTabIndicator();
}
});
const [inTransition] = useTransition();
createEffect(() => {
if (!inTransition()) {
requestAnimationFrame(recalculateTabIndicator); requestAnimationFrame(recalculateTabIndicator);
}
}); });
const isTabFocus = (idx: number) => { const isTabFocus = (idx: number) => {
@ -169,13 +184,13 @@ const Home: ParentComponent = (props) => {
<AppTopBar> <AppTopBar>
<Tabs> <Tabs>
<Tab focus={isTabFocus(0)} onClick={[onTabClick, 0]}> <Tab focus={isTabFocus(0)} onClick={[onTabClick, 0]}>
Home {t("tabs.home")}
</Tab> </Tab>
<Tab focus={isTabFocus(1)} onClick={[onTabClick, 1]}> <Tab focus={isTabFocus(1)} onClick={[onTabClick, 1]}>
Trending {t("tabs.trending")}
</Tab> </Tab>
<Tab focus={isTabFocus(2)} onClick={[onTabClick, 2]}> <Tab focus={isTabFocus(2)} onClick={[onTabClick, 2]}>
Public {t("tabs.public")}
</Tab> </Tab>
</Tabs> </Tabs>
<ProfileMenuButton profile={profiles()[0]}> <ProfileMenuButton profile={profiles()[0]}>
@ -187,7 +202,7 @@ const Home: ParentComponent = (props) => {
) )
} }
> >
<ListItemText>Prefetch Toots</ListItemText> <ListItemText>{t("set.prefetch-toots")}</ListItemText>
<ListItemSecondaryAction> <ListItemSecondaryAction>
<Switch checked={prefetching()}></Switch> <Switch checked={prefetching()}></Switch>
</ListItemSecondaryAction> </ListItemSecondaryAction>

View file

@ -16,6 +16,12 @@ import {
} from "@suid/icons-material"; } from "@suid/icons-material";
import A from "~platform/A"; import A from "~platform/A";
import Menu, { createManagedMenuState } from "~material/Menu"; import Menu, { createManagedMenuState } from "~material/Menu";
import { createTranslator } from "~platform/i18n";
type StringRes = Record<
"nav.bookmarks" | "nav.likes" | "nav.lists" | "nav.settings",
string
>;
const ProfileMenuButton: ParentComponent<{ const ProfileMenuButton: ParentComponent<{
profile?: { profile?: {
@ -32,6 +38,10 @@ const ProfileMenuButton: ParentComponent<{
}> = (props) => { }> = (props) => {
const menuId = createUniqueId(); const menuId = createUniqueId();
const buttonId = createUniqueId(); const buttonId = createUniqueId();
const [t] = createTranslator(
async (code) =>
(await import(`./i18n/${code}.json`)) as { default: StringRes },
);
const [open, state] = createManagedMenuState(); const [open, state] = createManagedMenuState();
@ -84,19 +94,19 @@ const ProfileMenuButton: ParentComponent<{
<ListItemIcon> <ListItemIcon>
<BookmarkIcon /> <BookmarkIcon />
</ListItemIcon> </ListItemIcon>
<ListItemText>Bookmarks</ListItemText> <ListItemText>{t("nav.bookmarks")}</ListItemText>
</MenuItem> </MenuItem>
<MenuItem disabled> <MenuItem disabled>
<ListItemIcon> <ListItemIcon>
<LikeIcon /> <LikeIcon />
</ListItemIcon> </ListItemIcon>
<ListItemText>Likes</ListItemText> <ListItemText>{t("nav.likes")}</ListItemText>
</MenuItem> </MenuItem>
<MenuItem disabled> <MenuItem disabled>
<ListItemIcon> <ListItemIcon>
<ListIcon /> <ListIcon />
</ListItemIcon> </ListItemIcon>
<ListItemText>Lists</ListItemText> <ListItemText>{t("nav.lists")}</ListItemText>
</MenuItem> </MenuItem>
<Divider /> <Divider />
<Show when={props.children}> <Show when={props.children}>
@ -107,7 +117,7 @@ const ProfileMenuButton: ParentComponent<{
<ListItemIcon> <ListItemIcon>
<SettingsIcon /> <SettingsIcon />
</ListItemIcon> </ListItemIcon>
<ListItemText>Settings</ListItemText> <ListItemText>{t("nav.settings")}</ListItemText>
</MenuItem> </MenuItem>
</Menu> </Menu>
</> </>

View file

@ -1,3 +1,15 @@
{ {
"Choose Language": "Choose Language" "Choose Language": "Choose Language",
"tabs.home": "Home",
"tabs.trending": "Trending",
"tabs.public": "Public",
"set.prefetch-toots": "Prefetch Toots",
"nav.bookmarks": "Bookmarks",
"nav.likes": "Likes",
"nav.lists": "Lists",
"nav.settings": "Settings"
} }

View file

@ -1,3 +1,14 @@
{ {
"Choose Language": "选择语言" "Choose Language": "选择语言",
"tabs.home": "主页",
"tabs.trending": "当下热门",
"tabs.public": "公开",
"set.prefetch-toots": "提前下载嘟文",
"nav.bookmarks": "所有书签",
"nav.likes": "喜欢的嘟文",
"nav.lists": "所有列表",
"nav.settings": "设置"
} }