mirror of
https://github.com/element-plus/element-plus.git
synced 2026-03-13 07:51:17 +08:00
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import { computed, inject, isRef, ref, unref } from 'vue'
|
|
import { get } from 'lodash-unified'
|
|
import English from '@element-plus/locale/lang/en'
|
|
|
|
import type { MaybeRef } from '@vueuse/core'
|
|
import type { InjectionKey, Ref } from 'vue'
|
|
import type { FieldPath } from '@element-plus/utils'
|
|
import type { Language } from '@element-plus/locale'
|
|
|
|
export type LocaleKeys =
|
|
| Exclude<FieldPath<typeof English>, 'name' | 'el'>
|
|
| (string & NonNullable<unknown>)
|
|
|
|
export type TranslatorOption = Record<string, string | number>
|
|
export type Translator = (path: LocaleKeys, option?: TranslatorOption) => string
|
|
export type LocaleContext = {
|
|
locale: Ref<Language>
|
|
lang: Ref<string>
|
|
t: Translator
|
|
}
|
|
|
|
export const buildTranslator =
|
|
(locale: MaybeRef<Language>): Translator =>
|
|
(path, option) =>
|
|
translate(path, option, unref(locale))
|
|
|
|
export const translate = (
|
|
path: LocaleKeys,
|
|
option: undefined | TranslatorOption,
|
|
locale: Language
|
|
): string =>
|
|
(get(locale, path, path) as string).replace(
|
|
/\{(\w+)\}/g,
|
|
(_, key) => `${option?.[key] ?? `{${key}}`}`
|
|
)
|
|
|
|
export const buildLocaleContext = (
|
|
locale: MaybeRef<Language>
|
|
): LocaleContext => {
|
|
const lang = computed(() => unref(locale).name)
|
|
const localeRef = isRef(locale) ? locale : ref(locale)
|
|
return {
|
|
lang,
|
|
locale: localeRef,
|
|
t: buildTranslator(locale),
|
|
}
|
|
}
|
|
|
|
export const localeContextKey: InjectionKey<Ref<Language | undefined>> =
|
|
Symbol('localeContextKey')
|
|
|
|
export const useLocale = (localeOverrides?: Ref<Language | undefined>) => {
|
|
const locale = localeOverrides || inject(localeContextKey, ref())!
|
|
return buildLocaleContext(computed(() => locale.value || English))
|
|
}
|