From 4c7a74c6c8a89e58e137982edd1f6a12c798262a Mon Sep 17 00:00:00 2001 From: shinya Date: Mon, 7 Apr 2025 01:17:25 +0800 Subject: [PATCH] feat: giscus i18n --- src/components/Comments/Giscus.astro | 20 ++++++++++++++++---- src/i18n/config.ts | 9 +++++++++ src/types/index.d.ts | 1 - 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/components/Comments/Giscus.astro b/src/components/Comments/Giscus.astro index 8212d73..ed3b9cf 100644 --- a/src/components/Comments/Giscus.astro +++ b/src/components/Comments/Giscus.astro @@ -1,6 +1,19 @@ --- -import { themeConfig } from "@/config"; +import { defaultLocale, themeConfig } from '@/config' +import { giscusLocaleMap } from '@/i18n/config' +// Get the language code of Giscus +function getGiscusLang(currentPath: string, defaultLocale: string): string { + // Extract language code from path + const pathLang = Object.keys(giscusLocaleMap).find(code => + currentPath.startsWith(`/${code}/`), + ) + // Return found path language or default language + const lang = pathLang || defaultLocale + return giscusLocaleMap[lang as keyof typeof giscusLocaleMap] +} + +const giscusLang = getGiscusLang(Astro.url.pathname, defaultLocale) const { repo = "", repoID = "", @@ -8,7 +21,6 @@ const { categoryID = "", mapping = "pathname", inputPosition = "top", - lang = "zh-CN", loading = "lazy", } = themeConfig.comment?.giscus ?? {}; --- @@ -24,7 +36,7 @@ const { categoryID, mapping, inputPosition, - lang, + giscusLang, loading, }} > @@ -53,7 +65,7 @@ const { script.setAttribute("data-theme", theme); script.setAttribute("data-emit-metadata", "0"); script.setAttribute("data-input-position", inputPosition); - script.setAttribute("data-lang", lang); + script.setAttribute("data-lang", giscusLang); script.setAttribute("data-loading", loading); script.crossOrigin = "anonymous"; script.async = true; diff --git a/src/i18n/config.ts b/src/i18n/config.ts index eed7232..6ef713e 100644 --- a/src/i18n/config.ts +++ b/src/i18n/config.ts @@ -19,5 +19,14 @@ export const walineLocaleMap: Record = { 'ru': 'ru-RU', } +export const giscusLocaleMap: Record = { + 'zh': 'zh-CN', + 'zh-tw': 'zh-TW', + 'ja': 'ja', + 'en': 'en', + 'es': 'es', + 'ru': 'ru', +} + // Supported Languages export const supportedLangs = Object.keys(langMap).flat() diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 5f736b0..1af097e 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -53,7 +53,6 @@ export interface ThemeConfig { categoryID: string mapping: 'pathname' | 'url' | 'title' inputPosition: 'top' | 'bottom' - lang: Exclude, loading: 'lazy' | 'embed' } }