UNPKG

vue-cesium

Version:
1 lines 2.72 kB
{"version":3,"file":"index.mjs","sources":["../../../../../packages/composables/use-locale/index.ts"],"sourcesContent":["/*\n * @Author: zouyaoji@https://github.com/zouyaoji\n * @Date: 2021-11-07 15:49:08\n * @LastEditTime: 2022-08-03 14:02:17\n * @LastEditors: zouyaoji\n * @Description:\n * @FilePath: \\vue-cesium@next\\packages\\composables\\use-locale\\index.ts\n */\nimport { computed, isRef, ref, unref } from 'vue'\nimport Chinese from '@vue-cesium/locale/lang/zh-hans'\nimport { get } from 'lodash-unified'\nimport type { Ref } from 'vue'\nimport type { Language } from '@vue-cesium/locale'\nimport { useGlobalConfig } from '../use-global-config'\nimport { MaybeRef } from '@vue-cesium/utils/types'\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 = (path: string, option: undefined | TranslatorOption, locale: Language): string =>\n (get(locale, path, path) as string).replace(/\\{(\\w+)\\}/g, (_, key) => `${option?.[key] ?? `{${key}}`}`)\n\nexport const 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 const useLocale = () => {\n const locale = useGlobalConfig('locale')\n return buildLocaleContext(computed(() => locale.value || Chinese))\n}\n"],"names":["Chinese"],"mappings":";;;;;;AAwBa,MAAA,eAAA,GACX,CAAC,MAAA,KACD,CAAC,IAAA,EAAM,MACL,KAAA,SAAA,CAAU,IAAM,EAAA,MAAA,EAAQ,KAAM,CAAA,MAAM,CAAC,EAAA;AAElC,MAAM,SAAY,GAAA,CAAC,IAAc,EAAA,MAAA,EAAsC,WAC3E,GAAI,CAAA,MAAA,EAAQ,IAAM,EAAA,IAAI,CAAa,CAAA,OAAA,CAAQ,YAAc,EAAA,CAAC,GAAG,GAAK,KAAA;AA9BrE,EAAA,IAAA,EAAA,CAAA;AA8BwE,EAAA,OAAA,CAAA,EAAA,CAAG,EAAS,GAAA,MAAA,IAAA,IAAA,GAAA,KAAA,CAAA,GAAA,MAAA,CAAA,GAAA,CAAA,KAAT,IAAiB,GAAA,EAAA,GAAA,CAAA,CAAA,EAAI,GAAG,CAAG,CAAA,CAAA,CAAA,CAAA,CAAA;AAAA,CAAE,EAAA;AAE3F,MAAA,kBAAA,GAAqB,CAAC,MAA8C,KAAA;AAC/E,EAAA,MAAM,OAAO,QAAS,CAAA,MAAM,KAAM,CAAA,MAAM,EAAE,IAAI,CAAA,CAAA;AAC9C,EAAA,MAAM,YAAY,KAAM,CAAA,MAAM,CAAI,GAAA,MAAA,GAAS,IAAI,MAAM,CAAA,CAAA;AACrD,EAAO,OAAA;AAAA,IACL,IAAA;AAAA,IACA,MAAQ,EAAA,SAAA;AAAA,IACR,CAAA,EAAG,gBAAgB,MAAM,CAAA;AAAA,GAC3B,CAAA;AACF,EAAA;AAEO,MAAM,YAAY,MAAM;AAC7B,EAAM,MAAA,MAAA,GAAS,gBAAgB,QAAQ,CAAA,CAAA;AACvC,EAAA,OAAO,mBAAmB,QAAS,CAAA,MAAM,MAAO,CAAA,KAAA,IAASA,WAAO,CAAC,CAAA,CAAA;AACnE;;;;"}