refactor: theme config

This commit is contained in:
radishzzz 2025-03-11 12:10:12 +00:00
parent 8f8cda2717
commit 696fcfac07
15 changed files with 224 additions and 293 deletions

View file

@ -8,21 +8,12 @@ export const langMap: Record<string, string[]> = {
'ru': ['ru-RU'],
}
// 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',
}
// Standard Language Code
// 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': {
@ -57,6 +48,17 @@ export const ui = {
},
}
// 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

View file

@ -3,16 +3,16 @@ import themeConfig from '@/config'
const defaultLocale = themeConfig.global.locale
const moreLocales = themeConfig.global.moreLocale
// Get the language code in the path
// Get the language code from the path
export function getLangFromPath(path: string) {
const lang = path.split('/')[1]
return moreLocales.includes(lang) ? lang : defaultLocale
}
// Get the localized path
export function getLocalizedPath(path: string, currentLang?: string) {
export function getLocalizedPath(path: string) {
const pathWithoutSlashes = path.replace(/^\/+|\/+$/g, '')
const lang = currentLang || getLangFromPath(path)
const lang = getLangFromPath(path)
if (pathWithoutSlashes === '') {
return lang === defaultLocale ? '/' : `/${lang}/`
@ -21,22 +21,20 @@ export function getLocalizedPath(path: string, currentLang?: string) {
return lang === defaultLocale ? `/${pathWithoutSlashes}/` : `/${lang}/${pathWithoutSlashes}/`
}
// Checking the current path to the page
export function isHomePage(path: string) {
return path === '/' || moreLocales.some(lang => path === `/${lang}/`)
}
export function isPostPage(path: string) {
// 简化检查路径是否包含posts
return path.includes('/posts/')
}
export function isTagPage(path: string) {
// 简化检查路径是否包含tags
return path.includes('/tags/')
}
export function isAboutPage(path: string) {
// 简化检查路径是否包含about
return path.includes('/about/')
}
@ -49,30 +47,6 @@ export function getPagePath(path: string) {
isPost: isPostPage(path),
isTag: isTagPage(path),
isAbout: isAboutPage(path),
getLocalizedPath: (targetPath: string) => getLocalizedPath(targetPath, currentLang),
getLocalizedPath: (targetPath: string) => getLocalizedPath(targetPath),
}
}
/**
*
* @param post
* @returns
*/
export function getAvailableLanguages(post?: { data?: { lang?: string } }) {
// 默认支持所有配置的语言
const defaultLangs = ['', ...themeConfig.global.moreLocale]
// 如果没有提供文章对象,返回所有语言
if (!post || !post.data) {
return defaultLangs
}
// 确定文章支持的语言
if (post.data.lang && typeof post.data.lang === 'string' && post.data.lang.trim() !== '') {
// 如果lang是字符串
return ['', post.data.lang]
}
// 如果没有指定或格式不对,返回所有语言
return defaultLangs
}