mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-18 12:11:49 +02:00
🚀 refactor: optimize i18n logic functions and function naming
This commit is contained in:
parent
74a2b9da1a
commit
1492ae07d2
7 changed files with 118 additions and 174 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue