98 lines
3.1 KiB
JavaScript
98 lines
3.1 KiB
JavaScript
|
// Composables
|
||
|
import { useProxiedModel } from "../../composables/proxiedModel.mjs"; // Utilities
|
||
|
import { ref, shallowRef, watch } from 'vue';
|
||
|
import { consoleError, consoleWarn, getObjectValueByPath } from "../../util/index.mjs"; // Locales
|
||
|
import en from "../en.mjs"; // Types
|
||
|
const LANG_PREFIX = '$vuetify.';
|
||
|
const replace = (str, params) => {
|
||
|
return str.replace(/\{(\d+)\}/g, (match, index) => {
|
||
|
return String(params[+index]);
|
||
|
});
|
||
|
};
|
||
|
const createTranslateFunction = (current, fallback, messages) => {
|
||
|
return function (key) {
|
||
|
for (var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
||
|
params[_key - 1] = arguments[_key];
|
||
|
}
|
||
|
if (!key.startsWith(LANG_PREFIX)) {
|
||
|
return replace(key, params);
|
||
|
}
|
||
|
const shortKey = key.replace(LANG_PREFIX, '');
|
||
|
const currentLocale = current.value && messages.value[current.value];
|
||
|
const fallbackLocale = fallback.value && messages.value[fallback.value];
|
||
|
let str = getObjectValueByPath(currentLocale, shortKey, null);
|
||
|
if (!str) {
|
||
|
consoleWarn(`Translation key "${key}" not found in "${current.value}", trying fallback locale`);
|
||
|
str = getObjectValueByPath(fallbackLocale, shortKey, null);
|
||
|
}
|
||
|
if (!str) {
|
||
|
consoleError(`Translation key "${key}" not found in fallback`);
|
||
|
str = key;
|
||
|
}
|
||
|
if (typeof str !== 'string') {
|
||
|
consoleError(`Translation key "${key}" has a non-string value`);
|
||
|
str = key;
|
||
|
}
|
||
|
return replace(str, params);
|
||
|
};
|
||
|
};
|
||
|
function createNumberFunction(current, fallback) {
|
||
|
return (value, options) => {
|
||
|
const numberFormat = new Intl.NumberFormat([current.value, fallback.value], options);
|
||
|
return numberFormat.format(value);
|
||
|
};
|
||
|
}
|
||
|
function useProvided(props, prop, provided) {
|
||
|
const internal = useProxiedModel(props, prop, props[prop] ?? provided.value);
|
||
|
|
||
|
// TODO: Remove when defaultValue works
|
||
|
internal.value = props[prop] ?? provided.value;
|
||
|
watch(provided, v => {
|
||
|
if (props[prop] == null) {
|
||
|
internal.value = provided.value;
|
||
|
}
|
||
|
});
|
||
|
return internal;
|
||
|
}
|
||
|
function createProvideFunction(state) {
|
||
|
return props => {
|
||
|
const current = useProvided(props, 'locale', state.current);
|
||
|
const fallback = useProvided(props, 'fallback', state.fallback);
|
||
|
const messages = useProvided(props, 'messages', state.messages);
|
||
|
return {
|
||
|
name: 'vuetify',
|
||
|
current,
|
||
|
fallback,
|
||
|
messages,
|
||
|
t: createTranslateFunction(current, fallback, messages),
|
||
|
n: createNumberFunction(current, fallback),
|
||
|
provide: createProvideFunction({
|
||
|
current,
|
||
|
fallback,
|
||
|
messages
|
||
|
})
|
||
|
};
|
||
|
};
|
||
|
}
|
||
|
export function createVuetifyAdapter(options) {
|
||
|
const current = shallowRef(options?.locale ?? 'en');
|
||
|
const fallback = shallowRef(options?.fallback ?? 'en');
|
||
|
const messages = ref({
|
||
|
en,
|
||
|
...options?.messages
|
||
|
});
|
||
|
return {
|
||
|
name: 'vuetify',
|
||
|
current,
|
||
|
fallback,
|
||
|
messages,
|
||
|
t: createTranslateFunction(current, fallback, messages),
|
||
|
n: createNumberFunction(current, fallback),
|
||
|
provide: createProvideFunction({
|
||
|
current,
|
||
|
fallback,
|
||
|
messages
|
||
|
})
|
||
|
};
|
||
|
}
|
||
|
//# sourceMappingURL=vuetify.mjs.map
|