vuestic-ui
Version:
Vue 3 UI Framework
55 lines (54 loc) • 1.52 kB
JavaScript
import { computed, getCurrentInstance } from "vue";
import { w as warn } from "../utils/console.mjs";
import { u as useGlobalConfig } from "./useGlobalConfig.mjs";
const isTranslationKey = (value) => value.startsWith("$t:");
const useTranslationProp = (defaultValue) => {
return { type: String, default: defaultValue };
};
const applyI18nTemplate = (key, values) => {
if (!values) {
return key;
}
Object.keys(values).forEach((valueKey) => {
key = key.replace(`{${valueKey}}`, String(values[valueKey]));
});
return key;
};
const useTranslation = () => {
const { globalConfig } = useGlobalConfig();
const config = computed(() => globalConfig.value.i18n);
function t(key, values) {
var _a;
const $t = (_a = getCurrentInstance()) == null ? void 0 : _a.appContext.config.globalProperties.$t;
if (typeof $t === "function") {
const translated2 = $t(`vuestic.${key}`, values);
if (translated2) {
return translated2;
}
}
const translated = config.value[key];
if (!translated) {
warn(`${key} not found in VuesticUI i18n config`);
return key;
}
return applyI18nTemplate(translated, values) || key;
}
function tp(key, values) {
if (!key) {
return "";
}
if (isTranslationKey(key)) {
return t(key.slice(3), values);
}
return applyI18nTemplate(key, values) || key;
}
return {
tp,
t
};
};
export {
useTranslationProp as a,
useTranslation as u
};
//# sourceMappingURL=useTranslation.mjs.map