{"version":3,"file":"index.mjs","sources":["../../../../../packages/hooks/use-locale/index.ts"],"sourcesContent":["import { computed, inject, ref, unref, isRef } from 'vue'\r\nimport get from 'lodash/get'\r\nimport Chinese from '@sgui-plus/locale/lang/zh-cn'\r\n\r\nimport type { InjectionKey, Ref } from 'vue'\r\nimport type { Language } from '@sgui-plus/locale'\r\nimport type { MaybeRef } from '@vueuse/core'\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 buildLocaleContext = (\r\n  locale: MaybeRef<Language>\r\n): 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 localeContextKey: InjectionKey<Ref<Language | undefined>> =\r\n  Symbol('localeContextKey')\r\n\r\nexport const useLocale = (localeOverrides?: Ref<Language | undefined>) => {\r\n  // const locale = useGlobalConfig('locale')\r\n  const locale = localeOverrides || inject(localeContextKey, ref())!\r\n  return buildLocaleContext(computed(() => locale.value || Chinese))\r\n}\r\n"],"names":[],"mappings":";;;;AAGY,MAAC,eAAe,GAAG,CAAC,MAAM,KAAK,CAAC,IAAI,EAAE,MAAM,KAAK,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,EAAE;AACxF,MAAC,SAAS,GAA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