{"version":3,"file":"index.mjs","sources":["../../../../../packages/hooks/use-locale/index.ts"],"sourcesContent":["import { computed, inject, isRef, ref, unref } from 'vue'\nimport { get } from 'lodash-unified'\nimport English from '@element-plus/locale/lang/en'\n\nimport type { MaybeRef } from '@vueuse/core'\nimport type { InjectionKey, Ref } from 'vue'\nimport type { FieldPath } from '@element-plus/utils'\nimport type { Language } from '@element-plus/locale'\n\nexport type LocaleKeys =\n  | Exclude<FieldPath<typeof English>, 'name' | 'el'>\n  | (string & NonNullable<unknown>)\n\nexport type TranslatorOption = Record<string, string | number>\nexport type Translator = (path: LocaleKeys, option?: TranslatorOption) => string\nexport type LocaleContext = {\n  locale: Ref<Language>\n  lang: Ref<string>\n  t: Translator\n}\n\nexport const buildTranslator =\n  (locale: MaybeRef<Language>): Translator =>\n  (path, option) =>\n    translate(path, option, unref(locale))\n\nexport const translate = (\n  path: LocaleKeys,\n  option: undefined | TranslatorOption,\n  locale: Language\n): string =>\n  (get(locale, path, path) as string).replace(\n    /\\{(\\w+)\\}/g,\n    (_, key) => `${option?.[key] ?? `{${key}}`}`\n  )\n\nexport const buildLocaleContext = (\n  locale: MaybeRef<Language>\n): LocaleContext => {\n  const lang = computed(() => unref(locale).name)\n  const localeRef = isRef(locale) ? locale : ref(locale)\n  return {\n    lang,\n    locale: localeRef,\n    t: buildTranslator(locale),\n  }\n}\n\nexport const localeContextKey: InjectionKey<Ref<Language | undefined>> =\n  Symbol('localeContextKey')\n\nexport const useLocale = (localeOverrides?: Ref<Language | undefined>) => {\n  const locale = localeOverrides || inject(localeContextKey, ref())!\n  return buildLocaleContext(computed(() => locale.value || English))\n}\n"],"names":[],"mappings":";;;;AAqBO,MAAM,eAAA,GACX,CAAC,MAAA,KACD,CAAC,IAAA,EAAM,MAAA,KACL,SAAA,CAAU,IAAA,EAAM,MAAA,EAAQ,KAAA,CAAM,MAAM,CAAC;AAElC,MAAM,SAAA,GAAY,CACvB,IAAA,EACA,MAAA,EACA,WAEC,GAAA,CAAI,MAAA,EAAQ,IAAA,EAAM,IAAI,CAAA,CAAa,OAAA;AAAA,EAClC,YAAA;AAAA,EACA,CAAC,GAAG,GAAA,KAAK;AAjCb,IAAA,IAAA,EAAA;AAiCgB,IAAA,OAAA,CAAA,EAAA,CAAG,EAAA,GAAA,MAAA,IAAA,IAAA,GAAA,MAAA,GAAA,MAAA,CAAS,GAAA,CAAA,KAAT,IAAA,GAAA,EAAA,GAAiB,CAAA,CAAA,EAAI,GAAG,CAAA,CAAA,CAAG,CAAA,CAAA;AAAA,EAAA;AAC5C;AAEK,MAAM,kBAAA,GAAqB,CAChC,MAAA,KACkB;AAClB,EAAA,MAAM,OAAO,QAAA,CAAS,MAAM,KAAA,CAAM,MAAM,EAAE,IAAI,CAAA;AAC9C,EAAA,MAAM,YAAY,KAAA,CAAM,MAAM,CAAA,GAAI,MAAA,GAAS,IAAI,MAAM,CAAA;AACrD,EAAA,OAAO;AAAA,IACL,IAAA;AAAA,IACA,MAAA,EAAQ,SAAA;AAAA,IACR,CAAA,EAAG,gBAAgB,MAAM;AAAA,GAC3B;AACF;AAEO,MAAM,gBAAA,0BACJ,kBAAkB;AAEpB,MAAM,SAAA,GAAY,CAAC,eAAA,KAAgD;AACxE,EAAA,MAAM,MAAA,GAAS,eAAA,IAAmB,MAAA,CAAO,gBAAA,EAAkB,KAAK,CAAA;AAChE,EAAA,OAAO,mBAAmB,QAAA,CAAS,MAAM,MAAA,CAAO,KAAA,IAAS,OAAO,CAAC,CAAA;AACnE;;;;"}