{"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 zh from '@xzx-design/locale/lang/zh-CN'\n\nimport type { MaybeRef } from '@vueuse/core'\nimport type { ComputedRef, InjectionKey, Ref } from 'vue'\nimport type { Language } from '@xzx-design/locale'\n\nexport type TranslatorOption = Record<string, string | number>\nexport type Translator = (path: string, 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: string,\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  const localeContext = computed(\n    () => locale.value || zh\n  ) as ComputedRef<Language>\n  return buildLocaleContext(localeContext)\n}\n"],"names":[],"mappings":";;;;AAgBa,MAAA,eAAA,GACX,CAAC,MAAA,KACD,CAAC,IAAA,EAAM,MACL,KAAA,SAAA,CAAU,IAAM,EAAA,MAAA,EAAQ,KAAM,CAAA,MAAM,CAAC,EAAA;AAE5B,MAAA,SAAA,GAAY,CACvB,IACA,EAAA,MAAA,EACA,WAEC,GAAI,CAAA,MAAA,EAAQ,IAAM,EAAA,IAAI,CAAa,CAAA,OAAA;AAAA,E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