UNPKG

@seemusic/ui-components

Version:

A Vue 3 UI Library. Uses Composable.

32 lines (31 loc) 949 B
import { inject, isRef, ref, computed, unref } from "vue"; import zhCN from "../locales/lang/zh-cn.mjs"; const localeContextKey = Symbol("sopLocaleContextKey"); const get = (object, path, defaultValue = "undefined") => { let newPath = []; newPath = path.replace(/\[/g, ".").replace(/\]/g, "").split("."); return newPath.reduce((a, b) => { return (a || {})[b]; }, object) || defaultValue; }; const getLocaleContent = (locale) => { return (path, option) => get(unref(locale), path).replace( /\{(\w+)\}/g, (_, key) => `${option == null ? void 0 : option[key]}` ); }; const useLocale = () => { const locale = inject(localeContextKey) || zhCN; const localeRef = isRef(locale) ? locale : ref(locale); const lang = computed(() => unref(locale).name); return { lang, localeRef, t: getLocaleContent(computed(() => locale)) }; }; export { localeContextKey, useLocale }; //# sourceMappingURL=useLocale.mjs.map