feat: complete language switching functionality and centralized page routing

This commit is contained in:
radishzzz 2025-03-14 03:30:56 +00:00
parent 4651828dd1
commit 05d3a8034b
26 changed files with 253 additions and 146 deletions

View file

@ -1,76 +0,0 @@
// Global Language Map
export const langMap: Record<string, string[]> = {
'zh': ['zh-CN'],
'zh-tw': ['zh-TW'],
'ja': ['ja-JP'],
'en': ['en-US'],
'es': ['es-ES'],
'ru': ['ru-RU'],
}
// Standard Language Code (Unused)
export const langCode = Object.values(langMap).flat()
// Abbreviated Language Code
export const langPath = Object.keys(langMap).flat()
// UI Translation
export const ui = {
'zh': {
posts: '文章',
tags: '标签',
about: '关于',
},
'zh-tw': {
posts: '文章',
tags: '標籤',
about: '關於',
},
'ja': {
posts: '記事',
tags: 'タグ',
about: '概要',
},
'en': {
posts: 'Posts',
tags: 'Tags',
about: 'About',
},
'es': {
posts: 'Artículos',
tags: 'Etiquetas',
about: 'Sobre',
},
'ru': {
posts: 'Посты',
tags: 'Теги',
about: 'О себе',
},
}
// Waline Language Map
// See more at https://waline.js.org/guide/i18n.html
export const walineLocaleMap: Record<string, string> = {
'zh': 'zh-CN',
'zh-tw': 'zh-TW',
'ja': 'jp-JP', // Waline uses jp-JP not ja-JP
'en': 'en-US',
'es': 'es-ES',
'ru': 'ru-RU',
}
/**
* Get the language code of Waline
* @param currentPath Current page path
* @param defaultLocale Default language
* @returns Corresponding Waline language code
*/
export function getWalineLang(currentPath: string, defaultLocale: string): string {
// Extract language code from path
const pathLang = Object.keys(walineLocaleMap).find(code =>
currentPath.startsWith(`/${code}/`),
)
// Return found path language or default language
const lang = pathLang || defaultLocale
return walineLocaleMap[lang as keyof typeof walineLocaleMap]
}