react-intlayer
Version:
Easily internationalize i18n your React applications with type-safe multilingual content management.
39 lines (36 loc) • 1.08 kB
JavaScript
'use client';
import { IntlayerClientContext } from "./IntlayerProvider.mjs";
import { getTranslation } from "@intlayer/core/interpreter";
import { useContext } from "react";
//#region src/client/t.ts
/**
* Client-side translation function that returns the translation of the provided multilang content.
*
* If the locale is not provided, it will use the locale from the client context.
*
* @param multilangContent - An object mapping locales to their respective content.
* @param locale - Optional locale to override the current context locale.
* @returns The translation for the specified locale.
*
* @example
* ```tsx
* import { t } from 'react-intlayer';
*
* const MyComponent = () => {
* const greeting = t({
* en: 'Hello',
* fr: 'Bonjour',
* es: 'Hola',
* });
*
* return <h1>{greeting}</h1>;
* };
* ```
*/
const t = (multilangContent, locale) => {
const { locale: currentLocale } = useContext(IntlayerClientContext) ?? {};
return getTranslation(multilangContent, locale ?? currentLocale);
};
//#endregion
export { t };
//# sourceMappingURL=t.mjs.map