feat: align frontend languages with posts api

This commit is contained in:
TerryM
2026-05-26 07:36:53 +08:00
parent 453abfcec7
commit e7a5952d58
19 changed files with 565 additions and 544 deletions

View File

@@ -1,10 +1,12 @@
import { useI18n } from "../../i18n";
import { LANG_OPTIONS, languageLabel } from "../../i18nLanguages";
import { typeFilterLabel } from "../../resourceTypeLabels";
const TYPE_FILTERS = [
"all",
"image",
"video",
"music",
"ppt",
"pdf",
"text",
@@ -12,14 +14,7 @@ const TYPE_FILTERS = [
"archive",
] as const;
const LANG_FILTERS = ["", "zh-TW", "zh-CN", "en"] as const;
function langLabel(t: (k: string) => string, code: string) {
if (!code) return t("filterLanguageAll");
if (code === "zh-TW") return t("lang_zh_TW");
if (code === "zh-CN") return t("lang_zh_CN");
return t("lang_en");
}
const LANG_FILTERS = ["", ...LANG_OPTIONS.map((x) => x.code)] as const;
export type FilterChipsProps = {
type: string;
@@ -70,7 +65,7 @@ export function FilterChips({
: "border-ark-line text-neutral-300 hover:border-ark-gold/50"
}`}
>
{langLabel(t, code)}
{languageLabel(t, code)}
</button>
);
})}

View File

@@ -26,8 +26,7 @@ export function MessageStream({ scope }: MessageStreamProps) {
const { items, isLoading, error, hasMore, loadMore, reset } =
usePostStream(params);
const groups = useGroupedByDay(items, lang);
const retryLabel =
lang === "zh-TW" ? "重試" : lang === "zh-CN" ? "重试" : "Retry";
const retryLabel = lang === "zh" ? "重试" : "Retry";
const sentinelRef = useRef<HTMLDivElement>(null);
const hasMoreRef = useRef(hasMore);

View File

@@ -8,7 +8,7 @@ function makePost(id: string, isoDate: string): Post {
id,
categoryId: 1,
categorySlug: "x",
language: "zh-CN",
language: "zh",
attachments: [],
isRecommended: false,
publishedAt: isoDate,
@@ -25,7 +25,7 @@ describe("useGroupedByDay", () => {
makePost("c", "2026-02-28T01:00:00.000Z"),
makePost("d", "2026-05-16T12:00:00.000Z"),
];
const { result } = renderHook(() => useGroupedByDay(posts, "zh-CN"));
const { result } = renderHook(() => useGroupedByDay(posts, "zh"));
expect(result.current.length).toBeGreaterThanOrEqual(2);
const allIds = result.current.flatMap((g) => g.items.map((p) => p.id));
expect(allIds).toEqual(["a", "b", "c", "d"]);
@@ -47,7 +47,7 @@ describe("useGroupedByDay", () => {
});
it("returns empty array for empty input", () => {
const { result } = renderHook(() => useGroupedByDay([], "zh-CN"));
const { result } = renderHook(() => useGroupedByDay([], "zh"));
expect(result.current).toEqual([]);
});
});

View File

@@ -8,9 +8,16 @@ export type DayGroup = {
};
function localeFor(lang: string): string {
if (lang === "zh-TW") return "zh-TW";
if (lang === "zh-CN") return "zh-CN";
return "en-US";
const locales: Record<string, string> = {
zh: "zh-CN",
en: "en-US",
ja: "ja-JP",
ko: "ko-KR",
vi: "vi-VN",
id: "id-ID",
ms: "ms-MY",
};
return locales[lang] ?? "en-US";
}
function dayKey(iso: string): string {
@@ -28,12 +35,10 @@ function dayLabel(iso: string, lang: string): string {
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate();
if (isSameDay(d, today)) {
if (lang === "en") return "Today";
return "今天";
return lang === "zh" ? "今天" : "Today";
}
if (isSameDay(d, yesterday)) {
if (lang === "en") return "Yesterday";
return "昨天";
return lang === "zh" ? "昨天" : "Yesterday";
}
return new Intl.DateTimeFormat(localeFor(lang), {
month: "long",

View File

@@ -35,6 +35,7 @@ function postMatchesType(post: Post, type: string): boolean {
return a.kind === "image" || a.mime.startsWith("image/");
if (type === "video")
return a.kind === "video" || a.mime.startsWith("video/");
if (type === "music") return a.mime.startsWith("audio/") || ext === "mp3";
if (type === "pdf") return ext === "pdf" || a.mime === "application/pdf";
if (type === "ppt")
return (

View File

@@ -1,7 +1,14 @@
function localeFor(lang: string): string {
if (lang === "zh-TW") return "zh-TW";
if (lang === "zh-CN") return "zh-CN";
return "en-US";
const locales: Record<string, string> = {
zh: "zh-CN",
en: "en-US",
ja: "ja-JP",
ko: "ko-KR",
vi: "vi-VN",
id: "id-ID",
ms: "ms-MY",
};
return locales[lang] ?? "en-US";
}
function formatDate(iso: string, lang: string): string {