--- createdAt: 2025-02-07 updatedAt: 2025-06-29 title: useDictionary Hook - React Intlayer Dokumentation description: Vollständige Anleitung zur Verwendung des useDictionary Hooks in React-Anwendungen mit Intlayer für eine effiziente Handhabung lokalisierter Inhalte ohne visuellen Editor. keywords: - useDictionary - React - hook - intlayer - lokalisierung - i18n - wörterbuch - übersetzung slugs: - doc - package - react-intlayer - useDictionary history: - version: 5.5.10 date: 2025-06-29 changes: Historie initialisiert --- # React-Integration: `useDictionary` Hook Dokumentation Dieser Abschnitt bietet eine detaillierte Anleitung zur Verwendung des `useDictionary` Hooks in React-Anwendungen, um eine effiziente Handhabung lokalisierter Inhalte ohne visuellen Editor zu ermöglichen. ## Importieren von `useDictionary` in React Der `useDictionary` Hook kann in React-Anwendungen integriert werden, indem er je nach Kontext importiert wird: - **Client-Komponente:** ```typescript codeFormat="typescript" import { useDictionary } from "react-intlayer"; // Wird in clientseitigen React-Komponenten verwendet ``` ```javascript codeFormat="esm" import { useDictionary } from "react-intlayer"; // Wird in clientseitigen React-Komponenten verwendet ``` ```javascript codeFormat="commonjs" const { useDictionary } = require("react-intlayer"); // Wird in clientseitigen React-Komponenten verwendet ``` - **Server-Komponente:** ```typescript codeFormat="typescript" import { useDictionary } from "react-intlayer/server"; // Wird in serverseitigen React-Komponenten verwendet ``` ```javascript codeFormat="esm" import { useDictionary } from "react-intlayer/server"; // Wird in serverseitigen React-Komponenten verwendet ``` ```javascript codeFormat="commonjs" const { useDictionary } = require("react-intlayer/server"); // Wird in serverseitigen React-Komponenten verwendet ``` ## Parameter Der Hook akzeptiert zwei Parameter: 1. **`dictionary`**: Ein deklariertes Wörterbuchobjekt, das lokalisierte Inhalte für bestimmte Schlüssel enthält. 2. **`locale`** (optional): Die gewünschte Locale. Standardmäßig wird die Locale des aktuellen Kontexts verwendet, wenn keine angegeben ist. ## Wörterbuch Alle Wörterbuchobjekte sollten in strukturierten Inhaltsdateien deklariert werden, um Typsicherheit zu gewährleisten und Laufzeitfehler zu vermeiden. Die [Einrichtungsanweisungen finden Sie hier](https://github.com/aymericzip/intlayer/blob/main/docs/docs/de/dictionary/get_started.md). Hier ist ein Beispiel für die Inhaltsdeklaration: ```typescript fileName="./component.content.ts" contentDeclarationFormat="typescript" import { t, type Dictionary } from "intlayer"; const componentContent = { key: "component-example", content: { title: t({ en: "Client Component Example", fr: "Exemple de composant client", es: "Ejemplo de componente cliente", }), content: t({ en: "This is the content of a client component example", fr: "Ceci est le contenu d'un exemple de composant client", es: "Este es el contenido de un ejemplo de componente cliente", }), }, } satisfies Dictionary; export default componentContent; ``` ```javascript fileName="./component.content.mjs" contentDeclarationFormat="esm" import { t } from "intlayer"; /** @type {import('intlayer').Dictionary} */ // Wörterbuchobjekt für die Komponente const componentContent = { key: "component-example", content: { title: t({ en: "Client Component Example", fr: "Exemple de composant client", es: "Ejemplo de componente cliente", }), content: t({ en: "This is the content of a client component example", fr: "Ceci est le contenu d'un exemple de composant client", es: "Este es el contenido de un ejemplo de componente cliente", }), }, }; export default componentContent; ``` ```javascript fileName="./component.content.cjs" contentDeclarationFormat="commonjs" const { t } = require("intlayer"); /** @type {import('intlayer').Dictionary} */ // Wörterbuchobjekt für die Komponente const componentContent = { key: "component-example", content: { title: t({ en: "Client Component Example", fr: "Exemple de composant client", es: "Ejemplo de componente cliente", }), content: t({ en: "This is the content of a client component example", fr: "Ceci est le contenu d'un exemple de composant client", es: "Este es el contenido de un ejemplo de componente cliente", }), }, }; module.exports = componentContent; ``` ```json fileName="./component.content.json" contentDeclarationFormat="json" { "$schema": "https://intlayer.org/schema.json", "key": "component-example", "content": { "title": { "nodeType": "translation", "translation": { "en": "Client Component Example", "fr": "Exemple de composant client", "es": "Ejemplo de componente cliente" } }, "content": { "nodeType": "translation", "translation": { "en": "This is the content of a client component example", "fr": "Ceci est le contenu d'un exemple de composant client", "es": "Este es el contenido de un ejemplo de componente cliente" } } } } ``` ## Beispiel für die Verwendung in React Nachfolgend ein Beispiel, wie der `useDictionary` Hook in einer React-Komponente verwendet wird: ```tsx fileName="./ComponentExample.tsx" codeFormat="typescript" import type { FC } from "react"; tsx fileName="./ComponentExample.tsx" codeFormat="typescript" import type { FC } from "react"; import { useDictionary } from "react-intlayer"; import componentContent from "./component.content"; const ComponentExample: FC = () => { const { title, content } = useDictionary(componentContent); return (
{content}
{content}
{content}
{content}
{content.content}
{content.content}
{content.content}