{"version":3,"file":"index.mjs","sources":["../../../../../../packages/hooks/use-locale/index.ts"],"sourcesContent":["import { computed, inject, isRef, ref, unref } from 'vue'\r\nimport { get } from 'lodash-unified'\r\nimport zhCn from '@m-eleplus-crud/locale/lang/zh-cn'\r\n\r\nimport type { InjectionKey, Ref } from 'vue'\r\nimport type { MaybeRef } from '@vueuse/core'\r\nimport type { Language } from '@m-eleplus-crud/locale'\r\n\r\nexport type TranslatorOption = Record<string, string | number>\r\nexport type Translator = (path: string, option?: TranslatorOption) => string\r\nexport type LocaleContext = {\r\n  locale: Ref<Language>\r\n  lang: Ref<string>\r\n  t: Translator\r\n}\r\n\r\nexport const buildTranslator =\r\n  (locale: MaybeRef<Language>): Translator =>\r\n  (path, option) =>\r\n    translate(path, option, unref(locale))\r\n\r\nexport const translate = (\r\n  path: string,\r\n  option: undefined | TranslatorOption,\r\n  locale: Language\r\n): string =>\r\n  (get(locale, path, path) as string).replace(\r\n    /\\{(\\w+)\\}/g,\r\n    (_, key) => `${option?.[key] ?? `{${key}}`}`\r\n  )\r\n\r\nexport const localeContextKey: InjectionKey<Ref<Language | undefined>> =\r\n  Symbol('localeContextKey')\r\n\r\nexport const buildLocaleContext = (locale: any): LocaleContext => {\r\n  const lang = computed(() => unref(locale).name)\r\n  const localeRef = isRef(locale) ? locale : ref(locale)\r\n  return {\r\n    lang,\r\n    locale: localeRef,\r\n    t: buildTranslator(locale),\r\n  }\r\n}\r\n\r\nexport const useLocale = (localeOverrides?: Ref<Language | undefined>) => {\r\n  const locale = localeOverrides || inject(localeContextKey, ref())!\r\n  return buildLocaleContext(computed(() => locale.value || zhCn))\r\n}\r\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,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