index.mjs 984 B

1234567891011121314151617181920212223242526
  1. import { unref, computed, isRef, ref } from 'vue';
  2. import { get } from 'lodash-unified';
  3. import English from '../../locale/lang/en.mjs';
  4. import { useGlobalConfig } from '../use-global-config/index.mjs';
  5. const buildTranslator = (locale) => (path, option) => translate(path, option, unref(locale));
  6. const translate = (path, option, locale) => get(locale, path, path).replace(/\{(\w+)\}/g, (_, key) => {
  7. var _a;
  8. return `${(_a = option == null ? void 0 : option[key]) != null ? _a : `{${key}}`}`;
  9. });
  10. const buildLocaleContext = (locale) => {
  11. const lang = computed(() => unref(locale).name);
  12. const localeRef = isRef(locale) ? locale : ref(locale);
  13. return {
  14. lang,
  15. locale: localeRef,
  16. t: buildTranslator(locale)
  17. };
  18. };
  19. const useLocale = () => {
  20. const locale = useGlobalConfig("locale");
  21. return buildLocaleContext(computed(() => locale.value || English));
  22. };
  23. export { buildLocaleContext, buildTranslator, translate, useLocale };
  24. //# sourceMappingURL=index.mjs.map