--- createdAt: 2024-03-07 updatedAt: 2025-09-30 title: جعل المكون متعدد اللغات (مكتبة i18n) في React و Next.js description: تعلم كيفية إعلان واسترجاع المحتوى المحلي لبناء مكون React أو Next.js متعدد اللغات باستخدام Intlayer. keywords: - i18n - component - react - multilingual - next.js - intlayer slugs: - doc - component - i18n applicationTemplate: https://github.com/aymericzip/intlayer-vite-react-template youtubeVideo: https://www.youtube.com/watch?v=dS9L7uJeak4 --- # كيفية جعل المكون متعدد اللغات (i18n) باستخدام Intlayer يوضح هذا الدليل الخطوات الأساسية لجعل مكون واجهة المستخدم متعدد اللغات في إعدادين شائعين: - React (Vite/SPA) - Next.js (App Router) ستقوم أولاً بإعلان المحتوى الخاص بك، ثم استرجاعه في مكونك. ## 1) إعلان المحتوى الخاص بك (مشترك بين React و Next.js) قم بإنشاء ملف إعلان المحتوى بالقرب من مكونك. هذا يحافظ على الترجمات قريبة من مكان استخدامها ويمكّن من الأمان النوعي. ```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 أيضًا إذا كنت تفضل ملفات التكوين. ```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) استرجع محتواك ### الحالة أ — تطبيق React (Vite/SPA) الطريقة الافتراضية: استخدم `useIntlayer` للاسترجاع بواسطة المفتاح. هذا يحافظ على مكونات خفيفة ومكتوبة بأنواع. ```tsx fileName="ComponentExample.tsx" import { useIntlayer } from "react-intlayer"; export function ComponentExample() { const content = useIntlayer("component-example"); return (
{content.description}
{content.description}
> ); } ``` بديل: يمكن لـ `useDictionary` قراءة كائن معلن بالكامل إذا كنت تفضل تجميع البنية في موقع الاستدعاء. ```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}