提交正式官网
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import type { SiteLocale } from '~/types/locale'
|
||||
|
||||
/** 去掉 /zh 前缀,得到英文默认路径 */
|
||||
export function stripLocalePrefix(path: string): string {
|
||||
if (path === '/zh') return '/'
|
||||
if (path.startsWith('/zh/')) return path.slice(3) || '/'
|
||||
return path
|
||||
}
|
||||
|
||||
/** 根据语言生成站内路径(英文默认,中文加 /zh 前缀) */
|
||||
export function toLocalePath(path: string, locale: SiteLocale): string {
|
||||
const base = stripLocalePrefix(path.startsWith('/') ? path : `/${path}`)
|
||||
if (locale === 'zh') {
|
||||
return base === '/' ? '/zh' : `/zh${base}`
|
||||
}
|
||||
return base
|
||||
}
|
||||
|
||||
export function isZhPath(path: string): boolean {
|
||||
return path === '/zh' || path.startsWith('/zh/')
|
||||
}
|
||||
|
||||
export function localeFromPath(path: string): SiteLocale {
|
||||
return isZhPath(path) ? 'zh' : 'en'
|
||||
}
|
||||
Reference in New Issue
Block a user