element-plus/packages/locale/index.ts

32 lines
647 B
TypeScript
Raw Normal View History

2020-08-03 19:16:13 +08:00
import defaultLang from './lang/en'
2020-07-28 20:18:11 +08:00
let lang = defaultLang
2020-09-16 14:46:41 +08:00
function template(str: string, option) {
if(!str || !option) return str
return str.replace(/\{(\w+)\}/g, (match, key) => {
return option[key]
})
}
2020-09-18 16:15:30 +08:00
export const t = (path:string, option?): string => {
2020-08-03 19:16:13 +08:00
let value
2020-07-28 20:18:11 +08:00
const array = path.split('.')
let current = lang
for (let i = 0, j = array.length; i < j; i++) {
const property = array[i]
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
}
return ''
}
2020-08-03 19:16:13 +08:00
export const use = (l): void => {
2020-07-28 20:18:11 +08:00
lang = l || lang
}
2020-08-03 19:16:13 +08:00
export default { use, t }