--- createdAt: 2024-03-07 updatedAt: 2025-09-30 title: Uczyń komponent wielojęzycznym (biblioteka i18n) w React i Next.js description: Dowiedz się, jak zadeklarować i pobrać zlokalizowaną treść, aby zbudować wielojęzyczny komponent React lub Next.js z Intlayer. keywords: - i18n - komponent - react - wielojęzyczny - next.js - intlayer slugs: - doc - component - i18n applicationTemplate: https://github.com/aymericzip/intlayer-vite-react-template youtubeVideo: https://www.youtube.com/watch?v=dS9L7uJeak4 --- # Jak uczynić komponent wielojęzycznym (i18n) za pomocą Intlayer Ten przewodnik pokazuje minimalne kroki, aby uczynić komponent UI wielojęzycznym w dwóch popularnych konfiguracjach: - React (Vite/SPA) - Next.js (App Router) Najpierw zadeklarujesz swoją treść, a następnie pobierzesz ją w swoim komponencie. ## 1) Zadeklaruj swoją treść (wspólne dla React i Next.js) Utwórz plik deklaracji treści obok swojego komponentu. Pozwala to trzymać tłumaczenia blisko miejsca ich użycia oraz zapewnia bezpieczeństwo typów. ```ts fileName="component.content.ts" import { t, type Dictionary } from "intlayer"; const componentContent = { key: "component-example", content: { title: t({ en: "Hello", fr: "Bonjour", es: "Hola", }), description: t({ en: "A multilingual React component", fr: "Un composant React multilingue", es: "Un componente React multilingüe", }), }, } satisfies Dictionary; export default componentContent; ``` Obsługiwany jest również format JSON, jeśli wolisz pliki konfiguracyjne. ```json fileName="component.content.json" { "$schema": "https://intlayer.org/schema.json", "key": "component-example", "content": { "title": { "nodeType": "translation", "translation": { "en": "Hello", "fr": "Bonjour", "es": "Hola" } }, "description": { "nodeType": "translation", "translation": { "en": "A multilingual React component", "fr": "Un composant React multilingue", "es": "Un componente React multilingüe" } } } } ``` ## 2) Pobierz swoją treść ### Przypadek A — aplikacja React (Vite/SPA) Domyślne podejście: użyj `useIntlayer`, aby pobrać treść po kluczu. Pozwala to utrzymać komponenty lekkie i typowane. ```tsx fileName="ComponentExample.tsx" import { useIntlayer } from "react-intlayer"; export function ComponentExample() { const content = useIntlayer("component-example"); return (
{content.description}
{content.description}
> ); } ``` Alternatywa: `useDictionary` może odczytać cały zadeklarowany obiekt, jeśli wolisz umieszczać strukturę w miejscu wywołania. ```tsx fileName="ComponentWithDictionary.tsx" import { useDictionary } from "react-intlayer"; import componentContent from "./component.content"; export function ComponentWithDictionary() { const { title, description } = useDictionary(componentContent); return ({description}
{content.description}
> ); } ``` ```tsx fileName="app/[locale]/example/ClientComponent.tsx" "use client"; import { useIntlayer } from "next-intlayer"; export function ClientComponent() { const content = useIntlayer("component-example"); return ({content.description}