index.mjs 983 B

1234567891011121314151617181920212223
  1. import en_default from "../../locale/lang/en.mjs";
  2. import { get } from "lodash-unified";
  3. import { computed, inject, isRef, ref, unref } from "vue";
  4. //#region ../../packages/hooks/use-locale/index.ts
  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) => `${option?.[key] ?? `{${key}}`}`);
  7. const buildLocaleContext = (locale) => {
  8. return {
  9. lang: computed(() => unref(locale).name),
  10. locale: isRef(locale) ? locale : ref(locale),
  11. t: buildTranslator(locale)
  12. };
  13. };
  14. const localeContextKey = Symbol("localeContextKey");
  15. const useLocale = (localeOverrides) => {
  16. const locale = localeOverrides || inject(localeContextKey, ref());
  17. return buildLocaleContext(computed(() => locale.value || en_default));
  18. };
  19. //#endregion
  20. export { buildLocaleContext, buildTranslator, localeContextKey, translate, useLocale };
  21. //# sourceMappingURL=index.mjs.map