mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 19:51:07 +02:00
fix: optimize the language switching order on the article page
This commit is contained in:
parent
355b044e9f
commit
a61b299b51
2 changed files with 20 additions and 5 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { defaultLocale } from '@/config'
|
import { defaultLocale, moreLocales } from '@/config'
|
||||||
import { getLangFromPath, getNextLang } from '@/i18n/lang'
|
import { getLangFromPath, getNextLang } from '@/i18n/lang'
|
||||||
import { cleanPath } from '@/utils/page'
|
import { cleanPath } from '@/utils/page'
|
||||||
|
|
||||||
|
@ -84,19 +84,26 @@ export function getPostNextLangUrl(currentPath: string, supportedLangs: string[]
|
||||||
return getNextLangUrl(currentPath)
|
return getNextLangUrl(currentPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 确保supportedLangs按照allLocales的顺序排序
|
||||||
|
const sortedLangs = [...supportedLangs].sort((a, b) => {
|
||||||
|
// 使用导入的allLocales变量
|
||||||
|
const allLocales = [defaultLocale, ...moreLocales]
|
||||||
|
return allLocales.indexOf(a) - allLocales.indexOf(b)
|
||||||
|
})
|
||||||
|
|
||||||
// 找到当前语言在支持的语言中的索引
|
// 找到当前语言在支持的语言中的索引
|
||||||
const currentIndex = supportedLangs.indexOf(currentLang)
|
const currentIndex = sortedLangs.indexOf(currentLang)
|
||||||
|
|
||||||
// 如果当前语言不在支持的语言中,或者路径是根路径,返回第一个支持的语言
|
// 如果当前语言不在支持的语言中,或者路径是根路径,返回第一个支持的语言
|
||||||
if (currentIndex === -1 || currentPath === '/') {
|
if (currentIndex === -1 || currentPath === '/') {
|
||||||
const nextLang = supportedLangs[0]
|
const nextLang = sortedLangs[0]
|
||||||
// 如果下一个语言是默认语言,返回根路径
|
// 如果下一个语言是默认语言,返回根路径
|
||||||
return nextLang === defaultLocale ? '/' : `/${nextLang}/`
|
return nextLang === defaultLocale ? '/' : `/${nextLang}/`
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算下一个语言的索引
|
// 计算下一个语言的索引
|
||||||
const nextIndex = (currentIndex + 1) % supportedLangs.length
|
const nextIndex = (currentIndex + 1) % sortedLangs.length
|
||||||
const nextLang = supportedLangs[nextIndex]
|
const nextLang = sortedLangs[nextIndex]
|
||||||
|
|
||||||
// 构建下一个语言的URL
|
// 构建下一个语言的URL
|
||||||
return buildNextLangUrl(currentPath, currentLang, nextLang)
|
return buildNextLangUrl(currentPath, currentLang, nextLang)
|
||||||
|
|
|
@ -38,6 +38,14 @@ export async function getStaticPaths() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 对每个文章的supportedLangs按照allLocales的顺序排序
|
||||||
|
Object.keys(slugToLangs).forEach((slug) => {
|
||||||
|
// 按照allLocales的顺序排序
|
||||||
|
slugToLangs[slug].sort((a, b) => {
|
||||||
|
return allLocales.indexOf(a) - allLocales.indexOf(b)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
// 定义路径数组的类型
|
// 定义路径数组的类型
|
||||||
type PathItem = {
|
type PathItem = {
|
||||||
params: { posts_slug: string }
|
params: { posts_slug: string }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue