vuetify.mjs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Composables
  2. import { useProxiedModel } from "../../composables/proxiedModel.mjs"; // Utilities
  3. import { ref, shallowRef, watch } from 'vue';
  4. import { consoleError, consoleWarn, getObjectValueByPath } from "../../util/index.mjs"; // Locales
  5. import en from "../en.mjs"; // Types
  6. const LANG_PREFIX = '$vuetify.';
  7. const replace = (str, params) => {
  8. return str.replace(/\{(\d+)\}/g, (match, index) => {
  9. return String(params[+index]);
  10. });
  11. };
  12. const createTranslateFunction = (current, fallback, messages) => {
  13. return function (key) {
  14. for (var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  15. params[_key - 1] = arguments[_key];
  16. }
  17. if (!key.startsWith(LANG_PREFIX)) {
  18. return replace(key, params);
  19. }
  20. const shortKey = key.replace(LANG_PREFIX, '');
  21. const currentLocale = current.value && messages.value[current.value];
  22. const fallbackLocale = fallback.value && messages.value[fallback.value];
  23. let str = getObjectValueByPath(currentLocale, shortKey, null);
  24. if (!str) {
  25. consoleWarn(`Translation key "${key}" not found in "${current.value}", trying fallback locale`);
  26. str = getObjectValueByPath(fallbackLocale, shortKey, null);
  27. }
  28. if (!str) {
  29. consoleError(`Translation key "${key}" not found in fallback`);
  30. str = key;
  31. }
  32. if (typeof str !== 'string') {
  33. consoleError(`Translation key "${key}" has a non-string value`);
  34. str = key;
  35. }
  36. return replace(str, params);
  37. };
  38. };
  39. function createNumberFunction(current, fallback) {
  40. return (value, options) => {
  41. const numberFormat = new Intl.NumberFormat([current.value, fallback.value], options);
  42. return numberFormat.format(value);
  43. };
  44. }
  45. function useProvided(props, prop, provided) {
  46. const internal = useProxiedModel(props, prop, props[prop] ?? provided.value);
  47. // TODO: Remove when defaultValue works
  48. internal.value = props[prop] ?? provided.value;
  49. watch(provided, v => {
  50. if (props[prop] == null) {
  51. internal.value = provided.value;
  52. }
  53. });
  54. return internal;
  55. }
  56. function createProvideFunction(state) {
  57. return props => {
  58. const current = useProvided(props, 'locale', state.current);
  59. const fallback = useProvided(props, 'fallback', state.fallback);
  60. const messages = useProvided(props, 'messages', state.messages);
  61. return {
  62. name: 'vuetify',
  63. current,
  64. fallback,
  65. messages,
  66. t: createTranslateFunction(current, fallback, messages),
  67. n: createNumberFunction(current, fallback),
  68. provide: createProvideFunction({
  69. current,
  70. fallback,
  71. messages
  72. })
  73. };
  74. };
  75. }
  76. export function createVuetifyAdapter(options) {
  77. const current = shallowRef(options?.locale ?? 'en');
  78. const fallback = shallowRef(options?.fallback ?? 'en');
  79. const messages = ref({
  80. en,
  81. ...options?.messages
  82. });
  83. return {
  84. name: 'vuetify',
  85. current,
  86. fallback,
  87. messages,
  88. t: createTranslateFunction(current, fallback, messages),
  89. n: createNumberFunction(current, fallback),
  90. provide: createProvideFunction({
  91. current,
  92. fallback,
  93. messages
  94. })
  95. };
  96. }
  97. //# sourceMappingURL=vuetify.mjs.map