From 698c411c7142c7c92e1340c6aa5614e77fc02c18 Mon Sep 17 00:00:00 2001 From: grassblock Date: Mon, 11 Aug 2025 16:06:14 +0800 Subject: [PATCH] feat: implement ui i18n support --- astro.config.mjs | 51 ++++++++++-------- src/components/BackToTop.astro | 7 ++- src/components/NewsLetter.astro | 13 +++-- src/components/ReplyViaEmail.astro | 7 ++- src/components/Search.astro | 9 +++- src/components/TableOfContents.astro | 8 ++- src/components/ThemeSwitcher.astro | 13 +++-- src/components/ThemeSwitcher@CSSOnly.astro | 6 ++- src/components/shortcodes/LinkCard.astro | 9 +++- .../shortcodes/ProtectedContent.astro | 9 +++- src/i18n/ui.ts | 54 +++++++++++++++++-- src/pages/blog.astro | 12 +++-- src/pages/blog/[...slug].astro | 18 +++++-- 13 files changed, 162 insertions(+), 54 deletions(-) diff --git a/astro.config.mjs b/astro.config.mjs index 19c2d18..7959354 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,10 +1,10 @@ -import { defineConfig } from 'astro/config'; +import {defineConfig} from 'astro/config'; import sitemap from '@astrojs/sitemap'; import mdx from '@astrojs/mdx'; -import { remarkWordCount } from './src/plugins/remark/wordcount.js'; +import {remarkWordCount} from './src/plugins/remark/wordcount.js'; import cloudflare from '@astrojs/cloudflare'; import remarkMath from "remark-math"; @@ -14,28 +14,33 @@ import partytown from '@astrojs/partytown'; import {remarkModifiedTime} from "./src/plugins/remark/modified-time.mjs"; export default defineConfig({ - site: 'https://terminal-blog.example.com', - base: '/', - trailingSlash: 'ignore', - redirects: { - // for the old routes still can be accessed - "/post/[...slug]": "/blog/[...slug]" - }, - - build: { - format: 'directory' - }, - - markdown: { - shikiConfig: { - theme: 'nord', - wrap: true + site: 'https://terminal-blog.example.com', + base: '/', + trailingSlash: 'ignore', + redirects: { + // for the old routes still can be accessed + "/post/[...slug]": "/blog/[...slug]" }, - remarkPlugins: [ remarkMath, remarkWordCount, remarkModifiedTime ], - rehypePlugins: [ rehypeKatex ] - }, - integrations: [sitemap(), mdx(), partytown()], + build: { + format: 'directory' + }, - adapter: cloudflare() + markdown: { + shikiConfig: { + theme: 'nord', + wrap: true + }, + remarkPlugins: [remarkMath, remarkWordCount, remarkModifiedTime], + rehypePlugins: [rehypeKatex] + }, + + i18n: { + locales: ["en", "zh_hans"], + defaultLocale: "en", + }, + + integrations: [sitemap(), mdx(), partytown()], + + adapter: cloudflare() }); \ No newline at end of file diff --git a/src/components/BackToTop.astro b/src/components/BackToTop.astro index 3fe2679..88a73a1 100644 --- a/src/components/BackToTop.astro +++ b/src/components/BackToTop.astro @@ -1,8 +1,13 @@ --- import {siteConfig} from "../config"; +import { getLangFromUrl, useTranslations, useTranslatedPath } from '../i18n/utils'; + +const lang = getLangFromUrl(Astro.url); +const t = useTranslations(lang); +const translatePath = useTranslatedPath(lang); const noscript = siteConfig.noClientJavaScript --- -{noscript ? : +{noscript ? :