feat: giscus i18n

This commit is contained in:
shinya 2025-04-07 01:17:25 +08:00
parent 81ca27689f
commit 4c7a74c6c8
No known key found for this signature in database
GPG key ID: A27DBCC544F50A01
3 changed files with 25 additions and 5 deletions

View file

@ -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;

View file

@ -19,5 +19,14 @@ export const walineLocaleMap: Record<string, string> = {
'ru': 'ru-RU',
}
export const giscusLocaleMap: Record<string, string> = {
'zh': 'zh-CN',
'zh-tw': 'zh-TW',
'ja': 'ja',
'en': 'en',
'es': 'es',
'ru': 'ru',
}
// Supported Languages
export const supportedLangs = Object.keys(langMap).flat()

View file

@ -53,7 +53,6 @@ export interface ThemeConfig {
categoryID: string
mapping: 'pathname' | 'url' | 'title'
inputPosition: 'top' | 'bottom'
lang: Exclude<typeof supportedLangs[number], 'zh-CN'>,
loading: 'lazy' | 'embed'
}
}