index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var lodashUnified = require('lodash-unified');
  5. var en = require('../../locale/lang/en.js');
  6. const buildTranslator = (locale) => (path, option) => translate(path, option, vue.unref(locale));
  7. const translate = (path, option, locale) => lodashUnified.get(locale, path, path).replace(
  8. /\{(\w+)\}/g,
  9. (_, key) => {
  10. var _a;
  11. return `${(_a = option == null ? void 0 : option[key]) != null ? _a : `{${key}}`}`;
  12. }
  13. );
  14. const buildLocaleContext = (locale) => {
  15. const lang = vue.computed(() => vue.unref(locale).name);
  16. const localeRef = vue.isRef(locale) ? locale : vue.ref(locale);
  17. return {
  18. lang,
  19. locale: localeRef,
  20. t: buildTranslator(locale)
  21. };
  22. };
  23. const localeContextKey = Symbol("localeContextKey");
  24. const useLocale = (localeOverrides) => {
  25. const locale = localeOverrides || vue.inject(localeContextKey, vue.ref());
  26. return buildLocaleContext(vue.computed(() => locale.value || en["default"]));
  27. };
  28. exports.buildLocaleContext = buildLocaleContext;
  29. exports.buildTranslator = buildTranslator;
  30. exports.localeContextKey = localeContextKey;
  31. exports.translate = translate;
  32. exports.useLocale = useLocale;
  33. //# sourceMappingURL=index.js.map