From 4c623030baac83931252b94d57f4b109aae1f20b Mon Sep 17 00:00:00 2001 From: ClovertaTheTrilobita Date: Tue, 24 Mar 2026 21:32:44 +0200 Subject: [PATCH] added basic lang --- public/fonts/MapleMono-Bold.ttf.woff2 | Bin 0 -> 77372 bytes public/fonts/MapleMono-Regular.ttf.woff2 | Bin 0 -> 74756 bytes src/components/Header.astro | 1 + src/components/Navigation.astro | 17 +++++++++----- src/i18n/en.ts | 16 +++++++++++++ src/i18n/index.ts | 28 +++++++++++++++++++++++ src/i18n/zh.ts | 16 +++++++++++++ src/layouts/BaseLayout.astro | 4 ++++ src/pages/tags/index.astro | 10 ++++---- src/styles/global.css | 18 ++++++++++++++- 10 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 public/fonts/MapleMono-Bold.ttf.woff2 create mode 100644 public/fonts/MapleMono-Regular.ttf.woff2 create mode 100644 src/i18n/en.ts create mode 100644 src/i18n/index.ts create mode 100644 src/i18n/zh.ts diff --git a/public/fonts/MapleMono-Bold.ttf.woff2 b/public/fonts/MapleMono-Bold.ttf.woff2 new file mode 100644 index 0000000000000000000000000000000000000000..964c256e169276e7a324ad3ae9204b812f288496 GIT binary patch literal 77372 zcmV)8K*qm!Pew8T0RR910WLfM3;+NC1Qj>{0WIYK2LJ#700000000000000000000 z0000Sf}w5(U;v^P5eN#3*+__{Yyma`Bm=w<3%p1G1Rw>mG6#X*EL*=^vjUzs#=ZBu zs()8;qitZoPLYz|Kpk7r<-=rv%&~tkNVBZ6Yj_(9`6wIq4cr_jhgyAGoihM)pZ)*; z|NsC0|Nk44McC%dE{ZJIB+G+u5!2*aWhbUAgG$}?2l4FevNs?0A^^&3# z8J6S7)ugPl&azh4Z6Ia41}3|rtwF=w?7H2G2!A(S?z_S5YA?ZLP!FV zM4gFgJ~9TA_&n|-dZ84;LKF!XbPZHqeP$QIG>!3{=M#poF_FjSMZzj zeo{#~2{zA0ImQA>I1w5l30pc|)MfL&8g%jR1M-}Ac`Yweyqm6b&q;b)_I=+kKeo5s zcrW*o#U#ZcUGrd){-luF<)TPVY9UpE^j%GttUgz0{#w;I%p1wLdUMjji5}!MpF>S9 z->SIK{V%5acU#{Uc>#;ln|uMv1$93CAj${q+cJW|VMEdJAU|&ZUD(vq``GFQk)09<)Y-%U z6#>bcNnrqdr2&FgR{9_@=uhYCBPM2pX^EnLceS0Dte@)Iv56T!x0raq->`m+9O`EWglJVn|`tM|DSiW-{;(W zA9MkUF-GA*h|&Z^Q0xSe5yTRMCCwH^(@b-+{C=lpdVZ#7`tP;ox1Rj}Je$^i?|&YI zAR)#wlwlp0F_tkNm$)vm)H1|aYB`QI4r5uCIM!NY4Y91Dj^ns4>oLYyB$hgs7;7jY zln_IxHP$+Yby&-=PHL=0W320OT-LRW%kfx`>#cB`T9Ry`Vu-w`Z{$>C7~2Tl;hep)ta#&FG$W!!{)`$pU=9%YAyC(zx>0Onu9+1>Uw0)cqdoS#(Wb5$WcY55ZFq zLM(p5&e{F{>-hfZzr@W|Nok$dp}GPdfKPKrTnS9P2A+DZ1A;gH8h7#kV{?56GZ^Ig zh#>~VPz?~kQLNu#obI+#F1J0LcD~ -Tags + diff --git a/src/i18n/en.ts b/src/i18n/en.ts new file mode 100644 index 0000000..442a9f5 --- /dev/null +++ b/src/i18n/en.ts @@ -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", + }, +}; \ No newline at end of file diff --git a/src/i18n/index.ts b/src/i18n/index.ts new file mode 100644 index 0000000..d6ec186 --- /dev/null +++ b/src/i18n/index.ts @@ -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; + }; +} \ No newline at end of file diff --git a/src/i18n/zh.ts b/src/i18n/zh.ts new file mode 100644 index 0000000..8080101 --- /dev/null +++ b/src/i18n/zh.ts @@ -0,0 +1,16 @@ +export default { + nav: { + home: "首页", + posts: "文章", + tags: "标签", + about: "关于", + }, + post: { + writtenBy: "作者", + comments: "评论", + publishedOn: "发布于", + }, + theme: { + toggle: "切换主题", + }, +}; \ No newline at end of file diff --git a/src/layouts/BaseLayout.astro b/src/layouts/BaseLayout.astro index 51ecd43..8b21b7a 100644 --- a/src/layouts/BaseLayout.astro +++ b/src/layouts/BaseLayout.astro @@ -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); --- diff --git a/src/pages/tags/index.astro b/src/pages/tags/index.astro index 20ef854..0e03c3a 100644 --- a/src/pages/tags/index.astro +++ b/src/pages/tags/index.astro @@ -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"; --- diff --git a/src/styles/global.css b/src/styles/global.css index c621a7f..1abc1e0 100644 --- a/src/styles/global.css +++ b/src/styles/global.css @@ -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 {