🚀 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
}