mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-15 11:12:54 +02:00
feat: giscus i18n
This commit is contained in:
parent
81ca27689f
commit
4c7a74c6c8
3 changed files with 25 additions and 5 deletions
|
@ -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;
|
||||
|
|
|
@ -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()
|
||||
|
|
1
src/types/index.d.ts
vendored
1
src/types/index.d.ts
vendored
|
@ -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'
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue