import { computed, inject, isRef, ref, unref } from 'vue' import { get } from 'lodash-unified' import ZhCn from '@element-plus/locale/lang/zh-cn' import type { MaybeRef } from '@vueuse/core' import type { InjectionKey, Ref } from 'vue' import type { Language } from '@element-plus/locale' export type TranslatorOption = Record export type Translator = (path: string, option?: TranslatorOption) => string export type LocaleContext = { locale: Ref lang: Ref t: Translator } export const buildTranslator = (locale: MaybeRef): Translator => (path, option) => translate(path, option, unref(locale)) export const translate = ( path: string, option: undefined | TranslatorOption, locale: Language ): string => (get(locale, path, path) as string).replace( /\{(\w+)\}/g, (_, key) => `${option?.[key] ?? `{${key}}`}` ) export const buildLocaleContext = ( locale: MaybeRef ): 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> = Symbol('localeContextKey') export const useLocale = (localeOverrides?: Ref) => { const locale = localeOverrides || inject(localeContextKey, ref())! return buildLocaleContext(computed(() => locale.value || ZhCn)) }