react-intlayer
Version:
Easily internationalize i18n your React applications with type-safe multilingual content management.
41 lines (38 loc) • 1.11 kB
JavaScript
'use client';
import { getIntlayer } from "../getIntlayer.mjs";
import { IntlayerClientContext } from "./IntlayerProvider.mjs";
import { useContext, useMemo } from "react";
//#region src/client/useIntlayer.ts
/**
* Client-side hook that picks one dictionary by its key and returns its content.
*
* If the locale is not provided, it will use the locale from the client context.
*
* @param key - The unique key of the dictionary to retrieve.
* @param locale - Optional locale to override the current context locale.
* @returns The dictionary content for the specified locale.
*
* @example
* ```tsx
* import { useIntlayer } from 'react-intlayer';
*
* const MyComponent = () => {
* const content = useIntlayer('my-dictionary-key');
*
* return <div>{content.myField.value}</div>;
* };
* ```
*/
const useIntlayer = (key, locale) => {
const { locale: currentLocale } = useContext(IntlayerClientContext) ?? {};
return useMemo(() => {
return getIntlayer(key, locale ?? currentLocale);
}, [
key,
currentLocale,
locale
]);
};
//#endregion
export { useIntlayer };
//# sourceMappingURL=useIntlayer.mjs.map