استخدم مساعدك المفضل للملخص واستخدم هذه الصفحة والموفر AI الذي تريده
تاريخ الإصدارات
- "تحديث استخدام واجهة برمجة تطبيقات useIntlayer في Solid للوصول المباشر إلى الخصائص"v8.9.04/5/2026
- "إضافة أمر init"v7.5.930/12/2025
- "تحديث تكامل وتهيئة واستخدام Astro"v6.2.03/10/2025
تمت ترجمة محتوى هذه الصفحة باستخدام الذكاء الاصطناعي.
اعرض آخر نسخة المحتوى الأصلي باللغة الإنكليزيةإذا كان لديك فكرة لتحسين هذه الوثيقة، فلا تتردد في المساهمة من خلال تقديم طلب سحب على GitHub.
رابط GitHub للتوثيقنسخ الـ Markdown من المستند إلى الحافظة
ترجمة موقع Astro الخاص بك باستخدام Intlayer | التدويل (i18n)
جدول المحتويات
ما هو Intlayer؟
Intlayer هي مكتبة تدويل (i18n) مبتكرة ومفتوحة المصدر مصممة لتبسيط دعم اللغات المتعددة في تطبيقات الويب الحديثة.
باستخدام Intlayer، يمكنك:
- إدارة الترجمات بسهولة: باستخدام قواميس تعريفية على مستوى المكون.
- توطين الميتا داتا والمسارات والمحتوى ديناميكيًا.
- ضمان دعم TypeScript: من خلال أنواع مولدة تلقائيًا لتعزيز الإكمال التلقائي واكتشاف الأخطاء.
- الاستفادة من الميزات المتقدمة: مثل الكشف الديناميكي عن اللغة وتبديل اللغة.
دليل خطوة بخطوة لتهيئة Intlayer في Astro
تحقق من نموذج التطبيق على GitHub.
الخطوة 1: تثبيت التبعيات
قم بتثبيت الحزم اللازمة باستخدام مدير الحزم المفضل لديك:
نسخ الكود إلى الحافظة
npm install intlayer astro-intlayer# اختياري: إذا كنت ستضيف دعم React islandsnpm install react react-dom react-intlayer @astrojs/reactintlayer الحزمة الأساسية التي توفر أدوات i18n لإدارة التكوين، الترجمات، تعريف المحتوى، التحويل، وأوامر CLI.
astro-intlayer تتضمن إضافة تكامل Astro لربط Intlayer بـ Vite bundler، بالإضافة إلى وسيط (middleware) لاكتشاف لغة المستخدم المفضلة، وإدارة ملفات تعريف الارتباط (cookies)، والتعامل مع إعادة توجيه الروابط.
الخطوة 2: تهيئة مشروعك
أنشئ ملف تكوين لتحديد لغات تطبيقك:
نسخ الكود إلى الحافظة
import { Locales, type IntlayerConfig } from "intlayer";const config: IntlayerConfig = { internationalization: { locales: [ Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH, // لغاتك الأخرى ], defaultLocale: Locales.ENGLISH, },};export default config;من خلال ملف التكوين هذا، يمكنك تهيئة الروابط المترجمة، وإعادة توجيه الوسيط، وأسماء الكوكيز، وموقع وامتدادات تعريفات المحتوى، وتعطيل سجلات Intlayer في وحدة التحكم، والمزيد. للحصول على قائمة كاملة بالمعلمات المتاحة، راجع توثيق التهيئة.
الخطوة 3: دمج Intlayer في تكوين Astro الخاص بك
أضف إضافة intlayer إلى تكوين Astro الخاص بك.
نسخ الكود إلى الحافظة
// @ts-checkimport { intlayer } from "astro-intlayer";import { defineConfig } from "astro/config";// https://astro.build/configexport default defineConfig({ integrations: [intlayer()],});تُستخدم إضافة intlayer() لدمج Intlayer مع Astro. وهي تضمن إنشاء ملفات تعريف المحتوى ومراقبتها في وضع التطوير. وتعرّف متغيرات بيئة Intlayer داخل تطبيق Astro وتوفر أسماء مستعارة لتحسين الأداء.
الخطوة 4: تعريف المحتوى الخاص بك
أنشئ وأدِر تعريفات المحتوى لتخزين الترجمات:
نسخ الكود إلى الحافظة
import { t, type Dictionary } from "intlayer";import type { ReactNode } from "react";const appContent = { key: "app", content: { title: t({ en: "Hello World", fr: "Bonjour le monde", es: "Hola mundo", ar: "مرحبا بالعالم", }), },} satisfies Dictionary;export default appContent;يمكن تعريف تعريفات المحتوى في أي مكان في تطبيقك، طالما أنها مدرجة فيcontentDir(افتراضيًا./src) وتطابق امتداد ملف تعريف المحتوى (افتراضيًا.content.{json,ts,tsx,js,jsx,mjs,cjs}).
لمزيد من المعلومات، راجع توثيق تعريف المحتوى.
الخطوة 5: استخدام المحتوى في Astro
يمكنك استهلاك القواميس مباشرة في ملفات .astro الخاصة بك باستخدام المساعدين الأساسيين المصدرين من intlayer.
نسخ الكود إلى الحافظة
---import { getIntlayer, getLocaleFromPath, getLocalizedUrl, defaultLocale, localeMap, getHTMLTextDir, type LocalesValues,} from "intlayer";import LocaleSwitcher from "../components/LocaleSwitcher.astro";// Get the current locale from the URL (e.g. /es/about -> 'es')const locale = getLocaleFromPath(Astro.url.pathname) as LocalesValues;// Get the content for the 'app' dictionaryconst { title } = getIntlayer("app", locale);---<!doctype html><html lang={locale} dir={getHTMLTextDir(locale)}> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <link rel="icon" type="image/svg+xml" href="/favicon.svg" /> <title>{title}</title> <!-- Canonical link: Tells search engines which is the primary version of this page --> <link rel="canonical" href={new URL(getLocalizedUrl(Astro.url.pathname, locale), Astro.site)} /> <!-- Hreflang: Tell Google about all localized versions --> { localeMap(({ locale: mapLocale }) => ( <link rel="alternate" hreflang={mapLocale} href={new URL( getLocalizedUrl(Astro.url.pathname, mapLocale), Astro.site )} /> )) } <!-- x-default: Fallback for users in unmatched languages --> <link rel="alternate" hreflang="x-default" href={new URL( getLocalizedUrl(Astro.url.pathname, defaultLocale), Astro.site )} /> </head> <body> <header> <LocaleSwitcher /> </header> <main> <h1>{title}</h1> </main> </body></html>الخطوة 6: التوجيه المترجم (Localized Routing)
أنشئ أجزاء مسار ديناميكية لخدمة الصفحات المترجمة (مثل src/pages/[locale]/index.astro):
نسخ الكود إلى الحافظة
---import { getIntlayer } from "intlayer";const { title } = getIntlayer('app');---<h1>{title}</h1>يضيف تكامل Astro وسيط Vite الذي يساعد في التوجيه الحساس للغة وتعريفات البيئة أثناء التطوير. يمكنك أيضًا إنشاء روابط عبر اللغات باستخدام منطقك الخاص أو أدوات intlayer مثل getLocalizedUrl.
الخطوة 7: استمر في استخدام إطارات العمل المفضلة لديك
استمر في بناء تطبيقك باستخدام إطار العمل الذي تختاره.
- Intlayer + React: Intlayer مع React
- Intlayer + Vue: Intlayer مع Vue
- Intlayer + Svelte: Intlayer مع Svelte
- Intlayer + Solid: Intlayer مع Solid
- Intlayer + Preact: Intlayer مع Preact
تكوين TypeScript
تستخدم Intlayer تقنية توسيع الوحدات (module augmentation) للاستفادة من TypeScript، مما يجعل برمجتك أكثر قوة.


تأكد من أن تكوين TypeScript الخاص بك يتضمن الأنواع المولدة تلقائيًا.
نسخ الكود إلى الحافظة
{ // ... تكوين TypeScript الحالي الخاص بك "include": [ // ... تكوين TypeScript الحالي الخاص بك ".intlayer/**/*.ts", // تضمين الأنواع المولدة تلقائيًا ],}تكوين Git
يوصى بتجاهل الملفات التي تنشئها Intlayer. هذا يتجنب إضافتها إلى مستودع Git الخاص بك.
للقيام بذلك، أضف التعليمات التالية إلى ملف .gitignore الخاص بك:
نسخ الكود إلى الحافظة
# تجاهل الملفات المولدة بواسطة Intlayer.intlayerإضافة VS Code
لتحسين تجربة التطوير الخاصة بك مع Intlayer، يمكنك تثبيت إضافة Intlayer الرسمية لـ VS Code.
التثبيت من VS Code Marketplace
توفر هذه الإضافة:
- إكمال تلقائي لمفاتيح الترجمة.
- اكتشاف الأخطاء في الوقت الفعلي للترجمات المفقودة.
- معاينة مضمنة للمحتوى المترجم.
- إجراءات سريعة لإنشاء وتحديث الترجمات بسهولة.
لمزيد من المعلومات حول استخدام الإضافة، راجع توثيق إضافة VS Code.
(Optional) Step 15: Extract the content of your components
If you have an existing codebase, transforming thousands of files can be time-consuming.
To ease this process, Intlayer propose a compiler / extractor to transform your components and extract the content.
To set it up, you can add a compiler section in your intlayer.config.ts file:
نسخ الكود إلى الحافظة
import { type IntlayerConfig } from "intlayer";
const config: IntlayerConfig = {
// ... Rest of your config
compiler: {
/**
* Indicates if the compiler should be enabled.
*/
enabled: true,
/**
* Defines the output files path
*/
output: ({ fileName, extension }) => `./${fileName}${extension}`,
/**
* Indicates if the components should be saved after being transformed.
*
* - If `true`, the compiler will rewrite the component file in the disk. So the transformation will be permanent, and the compiler will skip the transformation for the next process. That way, the compiler can transform the app, and then it can be removed.
*
* - If `false`, the compiler will inject the `useIntlayer()` function call into the code in the build output only, and keep the base codebase intact. The transformation will be done only in memory.
*/
saveComponents: false,
/**
* Dictionary key prefix
*/
dictionaryKeyPrefix: "",
},
};
export default config;Run the extractor to transform your components and extract the content
نسخ الكود إلى الحافظة
npx intlayer extractتعمق أكثر
إذا كنت تريد معرفة المزيد، يمكنك أيضًا تنفيذ المحرر المرئي أو استخدام نظام إدارة المحتوى (CMS) لإخراج محتواك خارجيًا.