UNPKG

ideaz-element

Version:

<p align="center"> <a href="" target="_blank" rel="noopener noreferrer"> <img src="./docs/public/logo.svg" alt="Ideaz Element" width="180" style="width: 180px;" /> </a> </p>

1 lines 2.36 kB
{"version":3,"file":"useLocale.mjs","sources":["../../../packages/hooks/useLocale.ts"],"sourcesContent":["import { get } from 'lodash-unified'\nimport CN from '@ideaz/locale/lang/zh-cn'\nimport type { MaybeRef } from '@vueuse/core'\nimport type { Ref } from 'vue'\n\nexport interface TranslatePair {\n [key: string]: string | string[] | TranslatePair\n}\n\nexport interface Language {\n name: string\n el?: TranslatePair\n}\nexport type TranslatorOption = Record<string, string | number>\nexport type Translator = (path: string, option?: TranslatorOption) => string\nexport interface LocaleContext {\n locale: Ref<Language>\n lang: Ref<string>\n t: Translator\n}\n\nexport function translate(path: string, option: undefined | TranslatorOption, locale: Language): string {\n return (get(locale, path, path) as string).replace(\n /\\{(\\w+)\\}/g,\n (_, key) => `${option?.[key] ?? `{${key}}`}`,\n )\n}\n\nexport function buildTranslator(locale: MaybeRef<Language>): Translator {\n return (path, option) =>\n translate(path, option, unref(locale))\n}\n\nexport function buildLocaleContext(locale: MaybeRef<Language>): 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 function useLocale(localeOverrides?: Ref<Language | undefined>) {\n const locale = localeOverrides || inject('locale', ref())!\n return buildLocaleContext(computed(() => locale.value || CN))\n}\n"],"names":["translate","path","option","locale","get","_","key","buildTranslator","unref","buildLocaleContext","lang","computed","localeRef","isRef","ref","useLocale","localeOverrides","inject","CN"],"mappings":";;;;AAqBgB,SAAAA,EAAAC,GAAAC,GAAAC,GAAA;AACd,SAAAC,EAAAD,GAAAF,GAAAA,CAAA,EAAA;AAAA,IAA2C;AAAA,IACzC,CAAAI,GAAAC,MAAA,IAAAJ,KAAA,gBAAAA,EAAAI,OAAA,IAAAA,CAAA,GAAA;AAAA,EAC0C;AAE9C;AAEO,SAAAC,EAAAJ,GAAA;AACL,SAAA,CAAAF,GAAAC,MAAAF,EAAAC,GAAAC,GAAAM,EAAAL,CAAA,CAAA;AAEF;AAEO,SAAAM,EAAAN,GAAA;AACL,QAAAO,IAAAC,EAAA,MAAAH,EAAAL,CAAA,EAAA,IAAA,GACAS,IAAAC,EAAAV,CAAA,IAAAA,IAAAW,EAAAX,CAAA;AACA,SAAA;AAAA,IAAO,MAAAO;AAAA,IACL,QAAAE;AAAA,IACQ,GAAAL,EAAAJ,CAAA;AAAA,EACiB;AAE7B;AAEO,SAAAY,EAAAC,GAAA;AACL,QAAAb,IAAAa,KAAAC,EAAA,UAAAH,EAAA,CAAA;AACA,SAAAL,EAAAE,EAAA,MAAAR,EAAA,SAAAe,CAAA,CAAA;AACF;"}