vue-i18n.mjs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Composables
  2. import { useProxiedModel } from "../../composables/proxiedModel.mjs"; // Utilities
  3. import { watch } from 'vue';
  4. // Types
  5. function useProvided(props, prop, provided) {
  6. const internal = useProxiedModel(props, prop);
  7. internal.value = props[prop] ?? provided.value;
  8. watch(provided, v => {
  9. if (props[prop] == null) {
  10. internal.value = v;
  11. }
  12. });
  13. return internal;
  14. }
  15. function createProvideFunction(data) {
  16. return props => {
  17. const current = useProvided(props, 'locale', data.current);
  18. const fallback = useProvided(props, 'fallback', data.fallback);
  19. const messages = useProvided(props, 'messages', data.messages);
  20. const i18n = data.useI18n({
  21. locale: current.value,
  22. fallbackLocale: fallback.value,
  23. messages: messages.value,
  24. useScope: 'local',
  25. legacy: false,
  26. inheritLocale: false
  27. });
  28. watch(current, v => {
  29. i18n.locale.value = v;
  30. });
  31. return {
  32. name: 'vue-i18n',
  33. current,
  34. fallback,
  35. messages,
  36. t: function (key) {
  37. for (var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  38. params[_key - 1] = arguments[_key];
  39. }
  40. return i18n.t(key, params);
  41. },
  42. n: i18n.n,
  43. provide: createProvideFunction({
  44. current,
  45. fallback,
  46. messages,
  47. useI18n: data.useI18n
  48. })
  49. };
  50. };
  51. }
  52. export function createVueI18nAdapter(_ref) {
  53. let {
  54. i18n,
  55. useI18n
  56. } = _ref;
  57. const current = i18n.global.locale;
  58. const fallback = i18n.global.fallbackLocale;
  59. const messages = i18n.global.messages;
  60. return {
  61. name: 'vue-i18n',
  62. current,
  63. fallback,
  64. messages,
  65. // @ts-expect-error Type instantiation is excessively deep and possibly infinite
  66. t: function (key) {
  67. for (var _len2 = arguments.length, params = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
  68. params[_key2 - 1] = arguments[_key2];
  69. }
  70. return i18n.global.t(key, params);
  71. },
  72. n: i18n.global.n,
  73. provide: createProvideFunction({
  74. current,
  75. fallback,
  76. messages,
  77. useI18n
  78. })
  79. };
  80. }
  81. //# sourceMappingURL=vue-i18n.mjs.map