🚀 refactor: optimize i18n logic functions and function naming

This commit is contained in:
radishzzz 2025-03-16 23:54:03 +00:00
parent 74a2b9da1a
commit 1492ae07d2
7 changed files with 118 additions and 174 deletions

View file

@ -2,50 +2,22 @@ import { allLocales, defaultLocale, moreLocales } from '@/config'
// Gets the language code from the current path
export function getLangFromPath(path: string) {
const secondaryLang = moreLocales.find(
const currentLang = moreLocales.find(
lang =>
path.startsWith(`/${lang}/`),
)
return secondaryLang || defaultLocale
return currentLang || defaultLocale
}
/**
*
* @param currentLang
* @returns
*/
export function getNextLang(currentLang: string): string {
// 获取默认语言和所有支持的语言
// 直接使用导入的变量
// 找到当前语言在列表中的索引
// Get the next language code in the global language cycle
export function getNextGlobalLang(currentLang: string): string {
// Get index of current language
const currentIndex = allLocales.indexOf(currentLang)
// 如果当前语言不在列表中,返回默认语言
if (currentIndex === -1) {
return defaultLocale
}
// 计算下一个语言的索引(循环)
// Calculate and return next language in cycle
const nextIndex = (currentIndex + 1) % allLocales.length
// 返回下一个语言代码
return allLocales[nextIndex]
}
/**
*
* @param lang
* @returns
*/
export function getSupportedLangs(lang?: string): string[] {
// 直接使用导入的变量
// 如果指定了语言且不为空
if (lang && typeof lang === 'string' && lang.trim() !== '') {
return [lang]
}
// 否则返回所有支持的语言
return allLocales
}

View file

@ -1,5 +1,5 @@
import { defaultLocale, moreLocales } from '@/config'
import { getLangFromPath, getNextLang } from '@/i18n/lang'
import { getLangFromPath, getNextGlobalLang } from '@/i18n/lang'
import { cleanPath } from '@/utils/page'
// Generates a localized path based on current language
@ -15,96 +15,67 @@ export function getLocalizedPath(path: string, currentLang?: string) {
}
/**
* URL
* @param currentPath
* @param currentLang
* @param nextLang
* @returns URL
* Build path for next language
* @param currentPath Current page path
* @param currentLang Current language code
* @param nextLang Next language code to switch to
* @returns Path for next language
*/
export function buildNextLangUrl(currentPath: string, currentLang: string, nextLang: string): string {
// 直接使用导入的变量
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
}
/**
* 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 getNextSupportedLangUrl(currentPath: string, supportedLangs: string[]): string {
// 从路径提取当前语言
const currentLang = getLangFromPath(currentPath)
// 如果没有提供支持的语言或列表为空,使用普通的语言切换
if (!supportedLangs || supportedLangs.length === 0) {
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 = sortedLangs.indexOf(currentLang)
// 如果当前语言不在支持的语言中,或者路径是根路径,返回第一个支持的语言
if (currentIndex === -1 || currentPath === '/') {
const nextLang = sortedLangs[0]
// 如果下一个语言是默认语言,返回根路径
export function buildNextLangPath(currentPath: string, currentLang: string, nextLang: string): string {
if (currentPath === '/') {
return nextLang === defaultLocale ? '/' : `/${nextLang}/`
}
// 计算下一个语言的索引
const nextIndex = (currentIndex + 1) % sortedLangs.length
const nextLang = sortedLangs[nextIndex]
// Build path based on language change
const nextPath = (() => {
if (nextLang === defaultLocale) {
return currentPath.replace(`/${currentLang}`, '') || '/'
}
// 构建下一个语言的URL
return buildNextLangUrl(currentPath, currentLang, nextLang)
if (currentLang === defaultLocale) {
return `/${nextLang}${currentPath}`
}
return currentPath.replace(`/${currentLang}`, `/${nextLang}`)
})()
return nextPath.endsWith('/') ? nextPath : `${nextPath}/`
}
/**
* Get next language path from global language list
* @param currentPath Current page path
* @returns Path for next supported language
*/
export function getNextGlobalLangPath(currentPath: string): string {
const currentLang = getLangFromPath(currentPath)
const nextLang = getNextGlobalLang(currentLang)
return buildNextLangPath(currentPath, currentLang, nextLang)
}
/**
* Get next language path from supported language list
* @param currentPath Current page path
* @param supportedLangs List of supported language codes
* @returns Path for next supported language
*/
export function getNextSupportedLangPath(currentPath: string, supportedLangs: string[]): string {
if (!supportedLangs.length) {
return getNextGlobalLangPath(currentPath)
}
// Sort supported languages by global priority
const langPriority = new Map(
[defaultLocale, ...moreLocales].map((lang, index) => [lang, index]),
)
const sortedLangs = [...supportedLangs].sort(
(a, b) => langPriority.get(a)! - langPriority.get(b)!,
)
// Get current language and next in cycle
const currentLang = getLangFromPath(currentPath)
const currentIndex = sortedLangs.indexOf(currentLang)
const nextLang = sortedLangs[(currentIndex + 1) % sortedLangs.length]
return buildNextLangPath(currentPath, currentLang, nextLang)
}