mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 19:51:07 +02:00
refactor: organize and categorize functions
This commit is contained in:
parent
f5526f8622
commit
d352b6fb65
13 changed files with 150 additions and 176 deletions
130
src/i18n/path.ts
130
src/i18n/path.ts
|
@ -1,18 +1,6 @@
|
|||
import { defaultLocale, moreLocales } from '@/i18n/config'
|
||||
|
||||
// Removes leading and trailing slashes from a path
|
||||
export function cleanPath(path: string) {
|
||||
return path.replace(/^\/|\/$/g, '')
|
||||
}
|
||||
|
||||
// Gets the language code from the current path
|
||||
export function getLangFromPath(path: string) {
|
||||
const secondaryLang = moreLocales.find(
|
||||
lang =>
|
||||
path.startsWith(`/${lang}/`),
|
||||
)
|
||||
return secondaryLang || defaultLocale
|
||||
}
|
||||
import { defaultLocale } from '@/i18n/config'
|
||||
import { getLangFromPath, getNextLang } from '@/i18n/lang'
|
||||
import { cleanPath } from '@/utils/page'
|
||||
|
||||
// Generates a localized path based on current language
|
||||
export function getLocalizedPath(path: string, currentLang?: string) {
|
||||
|
@ -26,34 +14,90 @@ export function getLocalizedPath(path: string, currentLang?: string) {
|
|||
return lang === defaultLocale ? `/${clean}/` : `/${lang}/${clean}/`
|
||||
}
|
||||
|
||||
// Checks if the current path is the home/post/tag/about page
|
||||
export function isHomePage(path: string) {
|
||||
const clean = cleanPath(path)
|
||||
return clean === '' || moreLocales.includes(clean)
|
||||
}
|
||||
export function isPostPage(path: string) {
|
||||
const clean = cleanPath(path)
|
||||
return clean.startsWith('posts') || moreLocales.some(lang => clean.startsWith(`${lang}/posts`))
|
||||
}
|
||||
export function isTagPage(path: string) {
|
||||
const clean = cleanPath(path)
|
||||
return clean.startsWith('tags') || moreLocales.some(lang => clean.startsWith(`${lang}/tags`))
|
||||
}
|
||||
export function isAboutPage(path: string) {
|
||||
const clean = cleanPath(path)
|
||||
return clean.startsWith('about') || moreLocales.some(lang => clean.startsWith(`${lang}/about`))
|
||||
}
|
||||
/**
|
||||
* 构建下一个语言的URL
|
||||
* @param currentPath 当前页面路径
|
||||
* @param currentLang 当前语言代码
|
||||
* @param nextLang 下一个语言代码
|
||||
* @returns 下一个语言的URL
|
||||
*/
|
||||
export function buildNextLangUrl(currentPath: string, currentLang: string, nextLang: string): string {
|
||||
// 直接使用导入的变量
|
||||
let nextUrl = ''
|
||||
|
||||
// Returns page context including language and page type information
|
||||
export function getPageInfo(path: string) {
|
||||
const currentLang = getLangFromPath(path)
|
||||
|
||||
return {
|
||||
currentLang,
|
||||
isHome: isHomePage(path),
|
||||
isPost: isPostPage(path),
|
||||
isTag: isTagPage(path),
|
||||
isAbout: isAboutPage(path),
|
||||
getLocalizedPath: (targetPath: string) => getLocalizedPath(targetPath, currentLang),
|
||||
if (nextLang === defaultLocale) {
|
||||
// 如果下一个是默认语言,移除语言代码
|
||||
nextUrl = currentPath.replace(`/${currentLang}`, '') || '/'
|
||||
}
|
||||
else {
|
||||
// 如果当前是默认语言(没有语言代码在路径中)
|
||||
if (currentLang === defaultLocale) {
|
||||
// 在路径前添加新的语言代码
|
||||
nextUrl = `/${nextLang}${currentPath}`
|
||||
}
|
||||
else {
|
||||
// 替换当前语言代码为新的语言代码
|
||||
nextUrl = currentPath.replace(`/${currentLang}`, `/${nextLang}`)
|
||||
}
|
||||
}
|
||||
|
||||
// 确保URL格式正确
|
||||
if (nextUrl === '')
|
||||
nextUrl = '/'
|
||||
|
||||
// 确保非根路径的URL末尾有斜杠
|
||||
if (nextUrl !== '/' && !nextUrl.endsWith('/')) {
|
||||
nextUrl = `${nextUrl}/`
|
||||
}
|
||||
|
||||
return nextUrl
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接从当前路径获取下一个语言的URL
|
||||
* @param currentPath 当前页面路径
|
||||
* @returns 下一个语言的URL
|
||||
*/
|
||||
export function getNextLangUrl(currentPath: string): string {
|
||||
// 从路径提取当前语言
|
||||
const currentLang = getLangFromPath(currentPath)
|
||||
|
||||
// 获取下一个语言
|
||||
const nextLang = getNextLang(currentLang)
|
||||
|
||||
// 构建下一个语言的URL
|
||||
return buildNextLangUrl(currentPath, currentLang, nextLang)
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据支持的语言列表获取下一个语言的URL
|
||||
* @param currentPath 当前路径
|
||||
* @param supportedLangs 文章支持的语言列表
|
||||
* @returns 下一个可用语言的URL
|
||||
*/
|
||||
export function getPostNextLangUrl(currentPath: string, supportedLangs: string[]): string {
|
||||
// 从路径提取当前语言
|
||||
const currentLang = getLangFromPath(currentPath)
|
||||
|
||||
// 如果没有提供支持的语言或列表为空,使用普通的语言切换
|
||||
if (!supportedLangs || supportedLangs.length === 0) {
|
||||
return getNextLangUrl(currentPath)
|
||||
}
|
||||
|
||||
// 找到当前语言在支持的语言中的索引
|
||||
const currentIndex = supportedLangs.indexOf(currentLang)
|
||||
|
||||
// 如果当前语言不在支持的语言中,或者路径是根路径,返回第一个支持的语言
|
||||
if (currentIndex === -1 || currentPath === '/') {
|
||||
const nextLang = supportedLangs[0]
|
||||
// 如果下一个语言是默认语言,返回根路径
|
||||
return nextLang === defaultLocale ? '/' : `/${nextLang}/`
|
||||
}
|
||||
|
||||
// 计算下一个语言的索引
|
||||
const nextIndex = (currentIndex + 1) % supportedLangs.length
|
||||
const nextLang = supportedLangs[nextIndex]
|
||||
|
||||
// 构建下一个语言的URL
|
||||
return buildNextLangUrl(currentPath, currentLang, nextLang)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue