fix: map chinese language requests to zh-CN
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { useCallback, useEffect, useRef, useState } from "react";
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
import { getJSON } from "../../../api";
|
import { getJSON } from "../../../api";
|
||||||
|
import { langQuery, type Lang } from "../../../i18n";
|
||||||
import { MOCK_POSTS } from "../../../mocks/mockPosts";
|
import { MOCK_POSTS } from "../../../mocks/mockPosts";
|
||||||
import type { Post, PostListResponse, PostScope } from "../../../types/post";
|
import type { Post, PostListResponse, PostScope } from "../../../types/post";
|
||||||
|
|
||||||
@@ -12,7 +13,7 @@ export type PostStreamParams = {
|
|||||||
scope: PostScope;
|
scope: PostScope;
|
||||||
type?: string;
|
type?: string;
|
||||||
language?: string;
|
language?: string;
|
||||||
lang: string;
|
lang: Lang;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type PostStreamResult = {
|
export type PostStreamResult = {
|
||||||
@@ -65,7 +66,7 @@ function filterMock(params: PostStreamParams): Post[] {
|
|||||||
|
|
||||||
function buildRealUrl(params: PostStreamParams, cursor?: string): string {
|
function buildRealUrl(params: PostStreamParams, cursor?: string): string {
|
||||||
const sp = new URLSearchParams();
|
const sp = new URLSearchParams();
|
||||||
sp.set("lang", params.lang);
|
sp.set("lang", langQuery(params.lang));
|
||||||
sp.set("limit", String(PAGE_SIZE));
|
sp.set("limit", String(PAGE_SIZE));
|
||||||
if (params.scope.kind === "category") sp.set("category", params.scope.slug);
|
if (params.scope.kind === "category") sp.set("category", params.scope.slug);
|
||||||
if (params.type && params.type !== "all") sp.set("type", params.type);
|
if (params.type && params.type !== "all") sp.set("type", params.type);
|
||||||
|
|||||||
@@ -383,5 +383,5 @@ export function useI18n() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function langQuery(lang: Lang) {
|
export function langQuery(lang: Lang) {
|
||||||
return lang;
|
return lang === "zh" ? "zh-CN" : lang;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { useEffect, useState } from "react";
|
|||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
import { getJSON, itemsOrEmpty, type Category } from "../api";
|
import { getJSON, itemsOrEmpty, type Category } from "../api";
|
||||||
import { MessageStream } from "../components/messageStream/MessageStream";
|
import { MessageStream } from "../components/messageStream/MessageStream";
|
||||||
import { useI18n } from "../i18n";
|
import { langQuery, useI18n } from "../i18n";
|
||||||
|
|
||||||
export function CategoryPage() {
|
export function CategoryPage() {
|
||||||
const { slug = "" } = useParams();
|
const { slug = "" } = useParams();
|
||||||
@@ -11,7 +11,9 @@ export function CategoryPage() {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!slug) return;
|
if (!slug) return;
|
||||||
getJSON<Category[]>(`/api/categories?lang=${encodeURIComponent(lang)}`)
|
getJSON<Category[]>(
|
||||||
|
`/api/categories?lang=${encodeURIComponent(langQuery(lang))}`,
|
||||||
|
)
|
||||||
.then((cats) =>
|
.then((cats) =>
|
||||||
setTitle(itemsOrEmpty(cats).find((x) => x.slug === slug)?.name ?? slug),
|
setTitle(itemsOrEmpty(cats).find((x) => x.slug === slug)?.name ?? slug),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import {
|
|||||||
} from "../components/LatestUpdateRow";
|
} from "../components/LatestUpdateRow";
|
||||||
import { RecommendedCard } from "../components/RecommendedCard";
|
import { RecommendedCard } from "../components/RecommendedCard";
|
||||||
import { SectionHeader } from "../components/SectionHeader";
|
import { SectionHeader } from "../components/SectionHeader";
|
||||||
import { useI18n } from "../i18n";
|
import { langQuery, useI18n } from "../i18n";
|
||||||
import { categoryCardLines } from "../utils/categoryDisplay";
|
import { categoryCardLines } from "../utils/categoryDisplay";
|
||||||
|
|
||||||
export function Home() {
|
export function Home() {
|
||||||
@@ -23,7 +23,7 @@ export function Home() {
|
|||||||
const [canScrollRec, setCanScrollRec] = useState(false);
|
const [canScrollRec, setCanScrollRec] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const q = `?lang=${encodeURIComponent(lang)}`;
|
const q = `?lang=${encodeURIComponent(langQuery(lang))}`;
|
||||||
Promise.all([
|
Promise.all([
|
||||||
getJSON<Category[]>(`/api/categories${q}`),
|
getJSON<Category[]>(`/api/categories${q}`),
|
||||||
getJSON<{ items: Resource[] }>(`/api/resources/recommended${q}&limit=12`),
|
getJSON<{ items: Resource[] }>(`/api/resources/recommended${q}&limit=12`),
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
postJSON,
|
postJSON,
|
||||||
type Resource,
|
type Resource,
|
||||||
} from "../api";
|
} from "../api";
|
||||||
import { useI18n } from "../i18n";
|
import { langQuery, useI18n } from "../i18n";
|
||||||
import { LANG_OPTIONS, languageLabel } from "../i18nLanguages";
|
import { LANG_OPTIONS, languageLabel } from "../i18nLanguages";
|
||||||
import { typeFilterLabel } from "../resourceTypeLabels";
|
import { typeFilterLabel } from "../resourceTypeLabels";
|
||||||
|
|
||||||
@@ -80,7 +80,7 @@ export function SearchPage() {
|
|||||||
|
|
||||||
const query = useMemo(() => {
|
const query = useMemo(() => {
|
||||||
const p = new URLSearchParams();
|
const p = new URLSearchParams();
|
||||||
p.set("lang", lang);
|
p.set("lang", langQuery(lang));
|
||||||
p.set("limit", "50");
|
p.set("limit", "50");
|
||||||
if (q) p.set("q", q);
|
if (q) p.set("q", q);
|
||||||
if (type && type !== "all") p.set("type", type);
|
if (type && type !== "all") p.set("type", type);
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ export function AdminResourceForm() {
|
|||||||
const [err, setErr] = useState<string | null>(null);
|
const [err, setErr] = useState<string | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getJSON<Category[]>("/api/categories?lang=zh")
|
getJSON<Category[]>("/api/categories?lang=zh-CN")
|
||||||
.then(setCats)
|
.then(setCats)
|
||||||
.catch(() => setCats([]));
|
.catch(() => setCats([]));
|
||||||
}, []);
|
}, []);
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export function AdminResources() {
|
|||||||
const [total, setTotal] = useState(0);
|
const [total, setTotal] = useState(0);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getJSON<Category[]>("/api/categories?lang=zh")
|
getJSON<Category[]>("/api/categories?lang=zh-CN")
|
||||||
.then((cats) => {
|
.then((cats) => {
|
||||||
const m: Record<number, string> = {};
|
const m: Record<number, string> = {};
|
||||||
for (const c of cats) m[c.id] = c.name;
|
for (const c of cats) m[c.id] = c.name;
|
||||||
|
|||||||
Reference in New Issue
Block a user