30 lines
731 B
TypeScript
30 lines
731 B
TypeScript
import { type loc, locales, fallbackLocale } from '@/config/locales'
|
|
|
|
export const __c = async (key: string | null | undefined, locale?: loc) => {
|
|
key = (key ?? '').trim()
|
|
if (key.length === 0) return key
|
|
|
|
if (!locales.includes(locale ??= fallbackLocale)) {
|
|
locale = fallbackLocale
|
|
}
|
|
|
|
let data: any = await import(`@/locales/custom.${locale}`).then(({ default: data }) => data).catch(() => false)
|
|
if (data === false) return key
|
|
|
|
const x = key.split('.')
|
|
let c: number = x.length
|
|
|
|
if (c === 1) {
|
|
return data.hasOwn(x[0]) && typeof data[x[0]] === 'string' ? data[x[0]] : key
|
|
}
|
|
|
|
for (let i in x) {
|
|
if (data.hasOwn(x[i])) {
|
|
data = data[x[i]]
|
|
c--
|
|
}
|
|
}
|
|
|
|
return c === 0 ? data : key
|
|
}
|