index.mjs 1.0 KB

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