react-intlayer
Version:
Easily internationalize i18n your React applications with type-safe multilingual content management.
34 lines • 893 B
JavaScript
"use client";
import { getIntlayer } from "intlayer";
import { useContext, useMemo } from "react";
import { IntlayerClientContext } from "./IntlayerProvider.mjs";
const useI18n = (namespace, locale) => {
const { locale: currentLocale } = useContext(IntlayerClientContext);
const localeTarget = useMemo(
() => locale ?? currentLocale,
[currentLocale, locale]
);
let dictionaryContent = useMemo(
() => getIntlayer(namespace, localeTarget),
[namespace, localeTarget]
);
const t = (path) => {
if (!path) {
return dictionaryContent;
}
const pathArray = path.split(".");
let current = dictionaryContent;
for (const key of pathArray) {
current = current?.[key];
if (current === void 0) {
return dictionaryContent;
}
}
return current;
};
return t;
};
export {
useI18n
};
//# sourceMappingURL=useI18n.mjs.map