2020-11-19 15:58:25 +08:00
|
|
|
import dayjs from 'dayjs'
|
2021-09-17 15:27:31 +08:00
|
|
|
import { debugWarn } from '@element-plus/utils/error'
|
2021-07-23 16:35:20 +08:00
|
|
|
import defaultLang from './lang/en'
|
2020-07-28 20:18:11 +08:00
|
|
|
|
2021-01-19 23:49:07 +08:00
|
|
|
export type TranslatePair = {
|
|
|
|
[key: string]: string | string[] | TranslatePair
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Language = {
|
|
|
|
name: string
|
|
|
|
el: TranslatePair
|
|
|
|
}
|
|
|
|
|
|
|
|
let lang: Language = defaultLang as Language
|
|
|
|
|
|
|
|
let i18nHandler: null | ((...args: any[]) => string) = null
|
|
|
|
|
|
|
|
export const i18n = (fn: (...args: any[]) => string) => {
|
|
|
|
i18nHandler = fn
|
|
|
|
}
|
2020-07-28 20:18:11 +08:00
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
export const restoreHandler = () => (i18nHandler = defaultTranslator)
|
2021-07-24 20:00:06 +08:00
|
|
|
|
2020-09-16 14:46:41 +08:00
|
|
|
function template(str: string, option) {
|
2021-07-24 20:00:06 +08:00
|
|
|
if (!str || !option) return str
|
2020-09-16 14:46:41 +08:00
|
|
|
|
2021-07-23 16:35:20 +08:00
|
|
|
return str.replace(/\{(\w+)\}/g, (_, key) => {
|
2020-09-16 14:46:41 +08:00
|
|
|
return option[key]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:35:20 +08:00
|
|
|
const defaultTranslator = (...args: any[]) => {
|
2021-01-19 23:49:07 +08:00
|
|
|
const [path, option] = args
|
2020-08-03 19:16:13 +08:00
|
|
|
let value
|
2020-07-28 20:18:11 +08:00
|
|
|
const array = path.split('.')
|
2021-07-23 23:02:16 +08:00
|
|
|
let current = lang
|
2020-07-28 20:18:11 +08:00
|
|
|
for (let i = 0, j = array.length; i < j; i++) {
|
|
|
|
const property = array[i]
|
2020-09-16 14:49:21 +08:00
|
|
|
value = current[property]
|
2020-09-16 14:46:41 +08:00
|
|
|
if (i === j - 1) return template(value, option)
|
2020-07-28 20:18:11 +08:00
|
|
|
if (!value) return ''
|
|
|
|
current = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:35:20 +08:00
|
|
|
export const t = (...args: any[]): string => {
|
|
|
|
if (i18nHandler) {
|
|
|
|
const translation = i18nHandler(...args)
|
2021-07-24 20:00:06 +08:00
|
|
|
return translation || defaultTranslator(...args)
|
2021-07-23 16:35:20 +08:00
|
|
|
}
|
|
|
|
return defaultTranslator(...args)
|
|
|
|
}
|
|
|
|
|
2021-07-23 23:02:16 +08:00
|
|
|
export const use = (l: Language): void => {
|
2021-09-09 11:49:48 +08:00
|
|
|
debugWarn(
|
|
|
|
'deprecation',
|
|
|
|
`:
|
2021-07-26 00:24:30 +08:00
|
|
|
The previous i18n usage is deprecated please update to
|
|
|
|
the new one to get reactive i18n translations, refer to:
|
2021-08-10 11:33:04 +08:00
|
|
|
https://element-plus.org/#/en-US/component/i18n
|
2021-09-09 11:49:48 +08:00
|
|
|
`
|
|
|
|
)
|
2021-07-26 00:24:30 +08:00
|
|
|
|
2020-07-28 20:18:11 +08:00
|
|
|
lang = l || lang
|
2020-11-19 15:58:25 +08:00
|
|
|
if (lang.name) {
|
|
|
|
dayjs.locale(lang.name)
|
|
|
|
}
|
2020-07-28 20:18:11 +08:00
|
|
|
}
|
|
|
|
|
2021-07-23 16:35:20 +08:00
|
|
|
export const setLocale = use
|