--- createdAt: 2024-03-07 updatedAt: 2025-09-30 title: Tạo một component đa ngôn ngữ (thư viện i18n) trong React và Next.js description: Tìm hiểu cách khai báo và lấy nội dung bản địa hóa để xây dựng một component React hoặc Next.js đa ngôn ngữ với Intlayer. keywords: - i18n - component - react - đa ngôn ngữ - next.js - intlayer slugs: - doc - component - i18n applicationTemplate: https://github.com/aymericzip/intlayer-vite-react-template youtubeVideo: https://www.youtube.com/watch?v=dS9L7uJeak4 --- # Cách tạo một component đa ngôn ngữ (i18n) với Intlayer Hướng dẫn này trình bày các bước tối thiểu để làm cho một component giao diện người dùng đa ngôn ngữ trong hai thiết lập phổ biến: - React (Vite/SPA) - Next.js (App Router) Bạn sẽ bắt đầu bằng cách khai báo nội dung, sau đó lấy nội dung đó trong component của bạn. ## 1) Khai báo nội dung của bạn (dùng chung cho React và Next.js) Tạo một file khai báo nội dung gần component của bạn. Điều này giữ cho các bản dịch gần với nơi chúng được sử dụng và cho phép an toàn kiểu dữ liệu. ```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; ``` JSON cũng được hỗ trợ nếu bạn thích dùng các file cấu hình. ```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) Lấy nội dung của bạn ### Trường hợp A — Ứng dụng React (Vite/SPA) Cách tiếp cận mặc định: sử dụng `useIntlayer` để lấy theo key. Điều này giữ cho các component gọn nhẹ và có kiểu dữ liệu. ```tsx fileName="ComponentExample.tsx" import { useIntlayer } from "react-intlayer"; export function ComponentExample() { const content = useIntlayer("component-example"); return (
{content.description}
{content.description}
> ); } ``` Phương án thay thế: `useDictionary` có thể đọc toàn bộ đối tượng đã khai báo nếu bạn thích đặt cấu trúc tại nơi gọi. ```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}