added basic lang

This commit is contained in:
ClovertaTheTrilobita 2026-03-24 21:32:44 +02:00
parent 56697aa0ca
commit 4c623030ba
10 changed files with 97 additions and 13 deletions

Binary file not shown.

Binary file not shown.

View file

@ -5,6 +5,7 @@ import ThemeIcon from "./ThemeIcon.astro";
<header>
<nav>
<h1>SanYeCao Blog</h1>
<div>
<ThemeIcon />
</div>

View file

@ -1,8 +1,13 @@
---
import { getLangFromUrl, useTranslations } from "@/i18n";
const langParam = Astro.url.searchParams.get("lang");
const lang = langParam === "en" ? "en" : "zh";
const t = useTranslations(lang);
---
---
<a href="/">Home</a>
<a href="/about/">About</a>
<!-- <a href="/blog/">Blog</a> -->
<a href="/tags/">Tags</a>
<nav>
<a href={`/?lang=${lang}`}>{t("nav.home")}</a>
<a href={`/about?lang=${lang}`}>{t("nav.about")}</a>
<a href={`/tags?lang=${lang}`}>{t("nav.tags")}</a>
</nav>

16
src/i18n/en.ts Normal file
View file

@ -0,0 +1,16 @@
export default {
nav: {
home: "Home",
posts: "Posts",
tags: "Tags",
about: "About",
},
post: {
writtenBy: "Written by",
comments: "Comments",
publishedOn: "Published on",
},
theme: {
toggle: "Toggle theme",
},
};

28
src/i18n/index.ts Normal file
View file

@ -0,0 +1,28 @@
import zh from "./zh.ts";
import en from "./en.ts";
export const languages = {
zh,
en,
};
export type Lang = keyof typeof languages;
export function getLangFromUrl(url: URL): Lang {
const lang = url.pathname.split("/")[1];
if (lang === "en") return "en";
return "zh";
}
export function useTranslations(lang: Lang) {
return function t(path: string) {
const keys = path.split(".");
let current: any = languages[lang];
for (const key of keys) {
current = current?.[key];
}
return current ?? path;
};
}

16
src/i18n/zh.ts Normal file
View file

@ -0,0 +1,16 @@
export default {
nav: {
home: "首页",
posts: "文章",
tags: "标签",
about: "关于",
},
post: {
writtenBy: "作者",
comments: "评论",
publishedOn: "发布于",
},
theme: {
toggle: "切换主题",
},
};

View file

@ -1,7 +1,11 @@
---
import Footer from "../components/Footer.astro";
import Header from "@/components/Header.astro";
import { getLangFromUrl, useTranslations } from "@/i18n";
const { pageTitle } = Astro.props;
const lang = getLangFromUrl(Astro.url);
const t = useTranslations(lang);
---
<!doctype html>

View file

@ -1,11 +1,9 @@
---
import BaseLayout from "@/layouts/BaseLayout.astro";
const allPosts = Object.values(
import.meta.glob("@/pages/posts/*.md", { eager: true }),
);
const tags = [
...new Set(allPosts.map((post: any) => post.frontmatter.tags).flat()),
];
import { getCollection } from "astro:content";
import "@/styles/global.css";
const allPosts = await getCollection("blog");
const tags = [...new Set(allPosts.map((post: any) => post.data.tags).flat())];
const pageTitle = "Tag Index";
---

View file

@ -1,6 +1,22 @@
@font-face {
font-family: "Maple Mono";
src: url("/fonts/MapleMono-Regular.ttf.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "Maple Mono";
src: url("/fonts/MapleMono-Bold.ttf.woff2") format("woff2");
font-weight: 700;
font-style: normal;
font-display: swap;
}
html {
/* background-color: #f1f5f9; */
/* font-family: sans-serif; */
font-family: "Maple Mono", monospace;
}
body {