MD وMDX
استخدم @better-translate/md عندما تعيش الوثائق أو مقالات المدونة أو صفحات المحتوى في ملفات .md أو .mdx مترجمة.
لا تحل هذه الحزمة محل المترجم الأساسي. تقرأ قواعد الإعداد المحلي من المترجم الذي أنشأته مسبقًا.
1. تثبيت الحزم
npm install @better-translate/core @better-translate/md2. إنشاء المترجم
أنشئ src/i18n.ts:
ts
1import { configureTranslations } from "@better-translate/core";23const en = {4 docs: {5 title: "Docs",6 },7} as const;89const es = {10 docs: {11 title: "Documentacion",12 },13} as const;1415export const translator = await configureTranslations({16 availableLocales: ["en", "es"] as const,17 defaultLocale: "en",18 fallbackLocale: "en",19 messages: { en, es },20});3. إنشاء مجلدات المحتوى
ضع مجلدًا لكل إعداد محلي تحت نفس الجذر:
txt
1content/docs/2 en/3 getting-started.mdx4 es/5 getting-started.mdx4. إنشاء مساعدات markdown
أنشئ src/docs.ts:
ts
1import { createMarkdownHelpers } from "@better-translate/md";23import { translator } from "./i18n";45export const docs = createMarkdownHelpers(translator, {6 rootDir: "./content/docs",7});5. قراءة مستند واحد
ts
1import { docs } from "./docs";23const englishDoc = await docs.getDocument("getting-started", {4 locale: "en",5});67const spanishDoc = await docs.getDocument("getting-started", {8 locale: "es",9});6. التجميع عند الحاجة للمخرجات
ts
1const compiled = await docs.compileDocument("getting-started", {2 locale: "en",3});45compiled.kind; // "md" or "mdx"6compiled.path;7compiled.usedFallback;متى تستخدم مساعدات الخادم
إذا كان تطبيقك يحلّ إعداد الطلب المحلي على الخادم مسبقًا، استخدم @better-translate/md/server ليتبع تحميل markdown نفس إعداد الطلب المحلي تلقائيًا.
توليد markdown مترجم تلقائيًا
استخدم خيار markdown.rootDir في واجهة سطر الأوامر لترجمة ملفات markdown: دليل واجهة سطر الأوامر