2021-09-04 19:29:28 +08:00
|
|
|
import { computed, getCurrentInstance, inject, provide, ref } from 'vue'
|
2021-07-25 15:26:00 +08:00
|
|
|
import English from '@element-plus/locale/lang/en'
|
2021-11-05 18:10:07 +08:00
|
|
|
import { buildProp, definePropType } from '@element-plus/utils/props'
|
|
|
|
import type { InjectionKey, Ref } from 'vue'
|
2021-07-25 15:26:00 +08:00
|
|
|
import type { Language } from '@element-plus/locale'
|
|
|
|
|
|
|
|
export const useLocaleProps = {
|
2021-11-05 18:10:07 +08:00
|
|
|
locale: buildProp({
|
|
|
|
type: definePropType<Language>(Object),
|
|
|
|
}),
|
2021-07-25 15:26:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type Translator = (...args: any[]) => string
|
|
|
|
|
|
|
|
export type LocaleContext = {
|
2021-07-26 00:24:30 +08:00
|
|
|
locale: Ref<Language>
|
|
|
|
lang: Ref<string>
|
2021-07-25 15:26:00 +08:00
|
|
|
t: Translator
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
export const LocaleInjectionKey =
|
|
|
|
'ElLocaleInjection' as unknown as InjectionKey<LocaleContext>
|
2021-07-25 15:26:00 +08:00
|
|
|
|
2021-07-28 12:20:23 +08:00
|
|
|
// this is meant to fix global methods like `ElMessage(opts)`, this way we can inject current locale
|
|
|
|
// into the component as default injection value.
|
|
|
|
// refer to: https://github.com/element-plus/element-plus/issues/2610#issuecomment-887965266
|
|
|
|
let localeObjCache: LocaleContext
|
|
|
|
|
2021-08-24 13:36:48 +08:00
|
|
|
function translate(path, option, current) {
|
|
|
|
const paths = path.split('.')
|
|
|
|
let value
|
|
|
|
for (let i = 0, j = paths.length; i < j; i++) {
|
|
|
|
const property = paths[i]
|
|
|
|
value = current[property]
|
|
|
|
if (i === j - 1) return template(value, option)
|
|
|
|
if (!value) return ''
|
|
|
|
current = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:26:00 +08:00
|
|
|
export const useLocale = () => {
|
2021-10-28 04:44:27 +08:00
|
|
|
const vm = getCurrentInstance()!
|
2021-07-25 15:26:00 +08:00
|
|
|
const props = vm.props as {
|
|
|
|
locale: Language
|
|
|
|
}
|
|
|
|
|
2021-07-26 00:24:30 +08:00
|
|
|
const locale = computed(() => props.locale || English)
|
|
|
|
const lang = computed(() => locale.value.name)
|
|
|
|
|
|
|
|
const _translator = (...args: any[]) => {
|
|
|
|
const [path, option] = args
|
2021-08-24 13:36:48 +08:00
|
|
|
return translate(path, option, locale.value)
|
2021-07-26 00:24:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const t = (...args: any[]) => {
|
2021-08-24 13:36:48 +08:00
|
|
|
return _translator(...args)
|
2021-07-26 00:24:30 +08:00
|
|
|
}
|
2021-07-25 15:26:00 +08:00
|
|
|
|
2021-07-28 12:20:23 +08:00
|
|
|
const provides = {
|
2021-07-26 00:24:30 +08:00
|
|
|
locale,
|
|
|
|
lang,
|
|
|
|
t,
|
2021-10-28 04:44:27 +08:00
|
|
|
} as LocaleContext
|
2021-07-28 12:20:23 +08:00
|
|
|
|
|
|
|
// this could be broken if someone tries to do following:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <config-provider :locale="lang1">
|
|
|
|
* <config-provider :locale="lang2">
|
|
|
|
* Something calls modal component.
|
|
|
|
* </config-provider>
|
|
|
|
* </config-provider>
|
|
|
|
*/
|
|
|
|
localeObjCache = provides
|
|
|
|
provide(LocaleInjectionKey, provides)
|
2021-07-25 15:26:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function template(str: string, option) {
|
|
|
|
if (!str || !option) return str
|
|
|
|
return str.replace(/\{(\w+)\}/g, (_, key) => {
|
|
|
|
return option[key]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:27:16 +08:00
|
|
|
export const localeProviderMaker = (locale = English) => {
|
|
|
|
const lang = ref(locale.name)
|
|
|
|
const localeRef = ref(locale)
|
2021-08-24 13:36:48 +08:00
|
|
|
return {
|
|
|
|
lang,
|
|
|
|
locale: localeRef,
|
|
|
|
t: (...args: any[]) => {
|
|
|
|
const [path, option] = args
|
|
|
|
return translate(path, option, localeRef.value)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 00:24:30 +08:00
|
|
|
export const useLocaleInject = () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
return inject(
|
|
|
|
LocaleInjectionKey,
|
|
|
|
localeObjCache || {
|
|
|
|
lang: ref(English.name),
|
|
|
|
locale: ref(English),
|
|
|
|
t: (...args) => {
|
|
|
|
const [path, option] = args
|
|
|
|
return translate(path, option, English)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2021-07-25 15:26:00 +08:00
|
|
|
}
|