Skip to content

aymericzip/intlayer-skills / skills/intlayer-react

intlayer-react

Verified

Integrates Intlayer internationalization with React component. Use when the user asks to "setup React i18n", create a new translated component, use the "useIntlayer" hook, or configure providers.

references/packages_react-intlayer_useI18n.md

View on GitHub
---
createdAt: 2025-08-23
updatedAt: 2026-09-27
priority: 5
title: useI18n Hook Documentation | react-intlayer
description: "Use useI18n in React to get a translation function scoped to a dictionary, handy when migrating from key-based libraries."
keywords:
  - useI18n
  - i18n
  - translation
  - dictionary
  - Intlayer
  - internationalization
  - documentation
  - Next.js
  - JavaScript
  - React
slugs:
  - doc
  - packages
  - react-intlayer
  - useI18n
history:
  - version: 9.4.0
    date: 2026-08-22
    changes: "Update to Next.js >= 9.4.0 architecture"
  - version: 6.0.0
    date: 2025-06-29
    changes: "Initial writing of `useI18n` hook documentation"
author: aymericzip
---

# React Integration: `useI18n` Hook Documentation

This section provides detailed guidance on how to use the `useI18n` hook within React applications, enabling efficient content localization.

## Importing `useI18n` in React

The `useI18n` hook can be imported and integrated into React applications according to the context as follows:

- **Client Components:**

  ```typescript codeFormat="typescript"
  import { useI18n } from "react-intlayer"; // Use in client-side React components
  ```

  ```javascript codeFormat="esm"
  import { useI18n } from "react-intlayer"; // Use in client-side React components
  ```

  ```javascript codeFormat="commonjs"
  const { useI18n } = require("react-intlayer"); // Use in client-side React components
  ```

- **Server Components:**

  ```typescript codeFormat="commonjs"
  import { useI18n } from "react-intlayer/server"; // Use in server-side React components
  ```

  ```javascript codeFormat="esm"
  import { useI18n } from "react-intlayer/server"; // Use in server-side React components
  ```

  ```javascript codeFormat="commonjs"
  const { useI18n } = require("react-intlayer/server"); // Use in server-side React components
  ```

## Parameters

This hook accepts two parameters:

1. **`namespace`**: A dictionary namespace to scope the translation keys.
2. **`locale`** (optional): The desired locale. If not specified, the context's locale will be used as default.

## Dictionary

All dictionary keys must be declared within content declaration files to enhance type safety and prevent errors. [Configuration instructions can be found here](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/dictionary/content_file.md).

- [Configuration instructions can be found here](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/dictionary/content_file.md)

## Usage Examples in React

Examples of using the `useI18n` hook within React components:

<Tabs>
 <Tab label='Intlayer >=9.4' value='>=9.4'>

```tsx fileName="src/App.tsx" codeFormat={["typescript", "esm"]}
import type { FC } from "react";
import { ClientComponentExample, ServerComponentExample } from "@components";
import { IntlayerProvider } from "react-intlayer";
import { useI18n } from "react-intlayer/server";
import { Locales } from "intlayer";

const App: FC<{ locale: Locales }> = ({ locale }) => {
  const t = useI18n("home-page", locale);

  return (
    <>
      <p>{t("introduction")}</p>
      <IntlayerProvider locale={locale}>
        <ClientComponentExample />
      </IntlayerProvider>
      <ServerComponentExample />
    </>
  );
};
```

 </Tab>
 <Tab label='Intlayer <9.4' value='<9.4'>

```tsx fileName="src/App.tsx" codeFormat={["typescript", "esm"]}
import type { FC } from "react";
import { ClientComponentExample, ServerComponentExample } from "@components";
import { IntlayerProvider } from "react-intlayer";
import { useI18n, IntlayerServerProvider } from "react-intlayer/server";
import { Locales } from "intlayer";

const App: FC<{ locale: Locales }> = ({ locale }) => {
  const t = useI18n("home-page", locale);

  return (
    <>
      <p>{t("introduction")}</p>
      <IntlayerProvider locale={locale}>
        <ClientComponentExample />
      </IntlayerProvider>
      <IntlayerServerProvider locale={locale}>
        <ServerComponentExample />
      </IntlayerServerProvider>
    </>
  );
};
```

 </Tab>
</Tabs>

```tsx fileName="src/components/ComponentExample.tsx" codeFormat={["typescript", "esm"]}
import type { FC } from "react";
import { useI18n } from "react-intlayer";

const ComponentExample: FC = () => {
  const t = useI18n("component-example");

  return (
    <div>
      <h1>{t("title")}</h1> {/* Display the title */}
      <p>{t("description")}</p> {/* Display the description */}
    </div>
  );
};
```

```tsx fileName="src/components/ServerComponentExample.tsx" codeFormat={["typescript", "esm"]}
import { useI18n } from "react-intlayer/server";

const ServerComponentExample = () => {
  const t = useI18n("server-component");

  return (
    <div>
      <h1>{t("title")}</h1> {/* Display the title */}
      <p>{t("description")}</p> {/* Display the description */}
    </div>
  );
};
```

## Attribute Handling

When localizing attributes, access the translation values appropriately:

```html
<!-- For accessibility attributes (e.g., aria-label), use .value since pure strings are required -->
<button aria-label={t("button.ariaLabel").value}>{t("button.text")}</button>
```

## Additional Resources

- **Intlayer Visual Editor**: For a more intuitive content management experience, refer to the visual editor documentation [here](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/intlayer_visual_editor.md).

This section specifically covers the integration of the `useI18n` hook in React applications, simplifying the localization process and ensuring content consistency across different locales.
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 321kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 197kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 197kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 197kUpdated Claude CodeCodexVerifiedInstall