@nuxt/ui
Version:
A UI Library for Modern Web Apps, powered by Vue & Tailwind CSS.
26 lines (25 loc) • 752 B
JavaScript
import { computed, isRef, ref, unref } from "vue";
import { get } from "./index.js";
export function buildTranslator(locale) {
return (path, option) => translate(path, option, unref(locale));
}
export function translate(path, option, locale) {
const prop = get(locale, `messages.${path}`, path);
return prop.replace(
/\{(\w+)\}/g,
(_, key) => `${option?.[key] ?? `{${key}}`}`
);
}
export function buildLocaleContext(locale) {
const lang = computed(() => unref(locale).name);
const code = computed(() => unref(locale).code);
const dir = computed(() => unref(locale).dir);
const localeRef = isRef(locale) ? locale : ref(locale);
return {
lang,
code,
dir,
locale: localeRef,
t: buildTranslator(locale)
};
}