test: language switcher and i18n refactor

This commit is contained in:
radishzzz 2025-03-14 00:51:54 +00:00
parent 19d7ba4905
commit 4651828dd1
10 changed files with 127 additions and 133 deletions

106
src/utils/i18n/lang.ts Normal file
View file

@ -0,0 +1,106 @@
import { themeConfig } from '@/config'
/**
*
* @param currentLang
* @returns
*/
export function getNextLang(currentLang: string): string {
// 获取默认语言和所有支持的语言
const defaultLocale = themeConfig.global.locale
const allLocales = [defaultLocale, ...themeConfig.global.moreLocale]
// 找到当前语言在列表中的索引
const currentIndex = allLocales.indexOf(currentLang)
// 如果当前语言不在列表中,返回默认语言
if (currentIndex === -1) {
return defaultLocale
}
// 计算下一个语言的索引(循环)
const nextIndex = (currentIndex + 1) % allLocales.length
// 返回下一个语言代码
return allLocales[nextIndex]
}
/**
* URL
* @param currentPath
* @param currentLang
* @param nextLang
* @returns URL
*/
export function buildNextLangUrl(currentPath: string, currentLang: string, nextLang: string): string {
const defaultLocale = themeConfig.global.locale
let nextUrl = ''
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
}
/**
*
* @param currentPath
* @returns
*/
export function getLangFromPath(currentPath: string): string {
const defaultLocale = themeConfig.global.locale
let currentLang = ''
// 检查路径是否以/xx/开始其中xx是支持的语言代码
for (const lang of themeConfig.global.moreLocale) {
if (currentPath.startsWith(`/${lang}/`) || currentPath === `/${lang}`) {
currentLang = lang
break
}
}
// 如果没有找到语言代码,则认为是默认语言
if (!currentLang) {
currentLang = defaultLocale
}
return currentLang
}
/**
* 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)
}