index.mjs 1.0 KB

1234567891011121314151617181920212223242526
  1. import { computed, unref, 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(/\{(\w+)\}/g, (_, key) => {
  6. var _a;
  7. return `${(_a = option == null ? void 0 : option[key]) != null ? _a : `{${key}}`}`;
  8. });
  9. const buildLocaleContext = (locale) => {
  10. const lang = computed(() => unref(locale).name);
  11. const localeRef = isRef(locale) ? locale : ref(locale);
  12. return {
  13. lang,
  14. locale: localeRef,
  15. t: buildTranslator(locale)
  16. };
  17. };
  18. const localeContextKey = Symbol("localeContextKey");
  19. const useLocale = (localeOverrides) => {
  20. const locale = localeOverrides || inject(localeContextKey, ref());
  21. return buildLocaleContext(computed(() => locale.value || English));
  22. };
  23. export { buildLocaleContext, buildTranslator, localeContextKey, translate, useLocale };
  24. //# sourceMappingURL=index.mjs.map