المؤلف:
    إنشاء:2025-02-07آخر تحديث:2026-10-08

    المحتوى الشرطي / الشرط في Intlayer

    كيف يعمل الشرط

    في Intlayer، يتم تحقيق المحتوى الشرطي من خلال وظيفة cond، التي تربط شروطًا محددة (عادةً قيم منطقية) بالمحتوى المقابل لها. يتيح لك هذا النهج اختيار المحتوى ديناميكيًا بناءً على شرط معين. عند دمجه مع React Intlayer أو Next Intlayer، يتم اختيار المحتوى المناسب تلقائيًا وفقًا للشرط المقدم أثناء وقت التشغيل.

    إعداد المحتوى الشرطي

    لإعداد المحتوى الشرطي في مشروع Intlayer الخاص بك، قم بإنشاء وحدة محتوى تتضمن تعريفاتك الشرطية. فيما يلي أمثلة بتنسيقات مختلفة.

    **/*.content.ts
    import { cond, type Dictionary } from "intlayer";
    
    const myConditionalContent = {
      key: "my_key",
      content: {
        myCondition: cond({
          true: "المحتوى الخاص بي عندما يكون الشرط صحيحًا",
          false: "المحتوى الخاص بي عندما يكون الشرط خاطئًا",
          fallback: "المحتوى الخاص بي عندما يفشل الشرط", // اختياري
        }),
      },
    } satisfies Dictionary;
    
    export default myConditionalContent;
    
    إذا لم يتم إعلان fallback، سيتم أخذ المفتاح الأخير المعلن كـ fallback إذا لم يتم التحقق من الشرط.

    استخدام المحتوى الشرطي مع React Intlayer

    لاستخدام المحتوى الشرطي داخل مكون React، استورد واستخدم خطاف useIntlayer من حزمة react-intlayer. يجلب هذا الخطاف المحتوى للمفتاح المحدد ويسمح لك بتمرير شرط لتحديد المخرجات المناسبة.

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const ConditionalComponent: FC = () => {
      const { myCondition } = useIntlayer("my_key");
    
      return (
        <div>
          <p>
            {
              /* Output: my content when it's true */
              myCondition(true)
            }
          </p>
          <p>
            {
              /* Output: my content when it's false */
              myCondition(false)
            }
          </p>
          <p>
            {
              /* Output: my content when the condition fails */
              myCondition("")
            }
          </p>
          <p>
            {
              /* Output: my content when the condition fails */
              myCondition(undefined)
            }
          </p>
        </div>
      );
    };
    
    export default ConditionalComponent;
    

    لاستخدام المحتوى الشرطي في مكونات عميل Next.js، استرجعه عبر خطاف useIntlayer. فيما يلي مثال:

    **/*.tsx
    "use client";
    
    import type { FC } from "react";
    import { useIntlayer } from "next-intlayer";
    
    const ConditionalComponent: FC = () => {
      const { myCondition } = useIntlayer("my_key");
    
      return (
        <div>
          <p>{myCondition(true)}</p>
          <p>{myCondition(false)}</p>
        </div>
      );
    };
    
    export default ConditionalComponent;
    

    لاستخدام المحتوى الشرطي في مكونات Vue، استرجعه عبر خطاف useIntlayer. فيما يلي مثال:

    **/*.vue
    <script setup lang="ts">
    import { useIntlayer } from "vue-intlayer";
    
    const { myCondition } = useIntlayer("my_key");
    </script>
    
    <template>
      <div>
        <p>{{ myCondition(true) }}</p>
        <p>{{ myCondition(false) }}</p>
      </div>
    </template>
    

    لاستخدام المحتوى الشرطي في مكونات Svelte، استرجعه عبر خطاف useIntlayer. يتم الوصول إلى المخزن باستخدام `--- createdAt: 2025-02-07 updatedAt: 2026-09-27 priority: 8 title: "المحتوى الشرطي في Intlayer" description: "اعرض محتوى مختلفًا حسب شرط منطقي باستخدام العقدة cond() في Intlayer، يُعرَّف مرة واحدة ويُحسم عند العرض." keywords:

    • محتوى شرطي
    • التصيير الديناميكي
    • وثائق
    • Intlayer
    • Next.js
    • JavaScript
    • React slugs:
    • doc
    • concept
    • content
    • condition author: aymericzip

    المحتوى الشرطي / الشرط في Intlayer

    كيف يعمل الشرط

    في Intlayer، يتم تحقيق المحتوى الشرطي من خلال وظيفة cond، التي تربط شروطًا محددة (عادةً قيم منطقية) بالمحتوى المقابل لها. يتيح لك هذا النهج اختيار المحتوى ديناميكيًا بناءً على شرط معين. عند دمجه مع React Intlayer أو Next Intlayer، يتم اختيار المحتوى المناسب تلقائيًا وفقًا للشرط المقدم أثناء وقت التشغيل.

    إعداد المحتوى الشرطي

    لإعداد المحتوى الشرطي في مشروع Intlayer الخاص بك، قم بإنشاء وحدة محتوى تتضمن تعريفاتك الشرطية. فيما يلي أمثلة بتنسيقات مختلفة.

    **/*.content.ts
    import { cond, type Dictionary } from "intlayer";
    
    const myConditionalContent = {
      key: "my_key",
      content: {
        myCondition: cond({
          true: "المحتوى الخاص بي عندما يكون الشرط صحيحًا",
          false: "المحتوى الخاص بي عندما يكون الشرط خاطئًا",
          fallback: "المحتوى الخاص بي عندما يفشل الشرط", // اختياري
        }),
      },
    } satisfies Dictionary;
    
    export default myConditionalContent;
    
    إذا لم يتم إعلان fallback، سيتم أخذ المفتاح الأخير المعلن كـ fallback إذا لم يتم التحقق من الشرط.

    استخدام المحتوى الشرطي مع React Intlayer

    لاستخدام المحتوى الشرطي داخل مكون React، استورد واستخدم خطاف useIntlayer من حزمة react-intlayer. يجلب هذا الخطاف المحتوى للمفتاح المحدد ويسمح لك بتمرير شرط لتحديد المخرجات المناسبة.

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const ConditionalComponent: FC = () => {
      const { myCondition } = useIntlayer("my_key");
    
      return (
        <div>
          <p>
            {
              /* Output: my content when it's true */
              myCondition(true)
            }
          </p>
          <p>
            {
              /* Output: my content when it's false */
              myCondition(false)
            }
          </p>
          <p>
            {
              /* Output: my content when the condition fails */
              myCondition("")
            }
          </p>
          <p>
            {
              /* Output: my content when the condition fails */
              myCondition(undefined)
            }
          </p>
        </div>
      );
    };
    
    export default ConditionalComponent;
    

    لاستخدام المحتوى الشرطي في مكونات عميل Next.js، استرجعه عبر خطاف useIntlayer. فيما يلي مثال:

    **/*.tsx
    "use client";
    
    import type { FC } from "react";
    import { useIntlayer } from "next-intlayer";
    
    const ConditionalComponent: FC = () => {
      const { myCondition } = useIntlayer("my_key");
    
      return (
        <div>
          <p>{myCondition(true)}</p>
          <p>{myCondition(false)}</p>
        </div>
      );
    };
    
    export default ConditionalComponent;
    

    لاستخدام المحتوى الشرطي في مكونات Vue، استرجعه عبر خطاف useIntlayer. فيما يلي مثال:

    **/*.vue
    <script setup lang="ts">
    import { useIntlayer } from "vue-intlayer";
    
    const { myCondition } = useIntlayer("my_key");
    </script>
    
    <template>
      <div>
        <p>{{ myCondition(true) }}</p>
        <p>{{ myCondition(false) }}</p>
      </div>
    </template>
    

    . فيما يلي مثال:

    **/*.svelte
    <script lang="ts">
    import { useIntlayer } from "svelte-intlayer";
    
    const content = useIntlayer("my_key");
    </script>
    
    <div>
      <p>{$content.myCondition(true)}</p>
      <p>{$content.myCondition(false)}</p>
    </div>
    

    لاستخدام المحتوى الشرطي في مكونات Preact، استرجعه عبر خطاف useIntlayer. فيما يلي مثال:

    **/*.tsx
    import type { FC } from "preact";
    import { useIntlayer } from "preact-intlayer";
    
    const ConditionalComponent: FC = () => {
      const { myCondition } = useIntlayer("my_key");
    
      return (
        <div>
          <p>{myCondition(true)}</p>
          <p>{myCondition(false)}</p>
        </div>
      );
    };
    
    export default ConditionalComponent;
    

    لاستخدام المحتوى الشرطي في مكونات SolidJS، استرجعه عبر خطاف useIntlayer. فيما يلي مثال:

    **/*.tsx
    import type { Component } from "solid-js";
    import { useIntlayer } from "solid-intlayer";
    
    const ConditionalComponent: Component = () => {
      const { myCondition } = useIntlayer("my_key");
    
      return (
        <div>
          <p>{myCondition(true)}</p>
          <p>{myCondition(false)}</p>
        </div>
      );
    };
    
    export default ConditionalComponent;
    

    لاستخدام المحتوى الشرطي في مكونات Angular، استرجعه عبر خطاف useIntlayer. فيما يلي مثال:

    app.component.ts
    import { Component } from "@angular/core";
    import { useIntlayer } from "angular-intlayer";
    
    @Component({
      selector: "app-conditional",
      template: `
        <div>
          <p>{{ content().myCondition(true) }}</p>
          <p>{{ content().myCondition(false) }}</p>
        </div>
      `,
    })
    export class ConditionalComponent {
      content = useIntlayer("my_key");
    }
    

    لاستخدام المحتوى الشرطي مع vanilla-intlayer، استرجعه عبر خطاف useIntlayer. فيما يلي مثال:

    **/*.ts
    import { installIntlayer, useIntlayer } from "vanilla-intlayer";
    
    installIntlayer();
    
    const content = useIntlayer("my_key").onChange((newContent) => {
      document.getElementById("true-content")!.textContent =
        newContent.myCondition(true);
      document.getElementById("false-content")!.textContent =
        newContent.myCondition(false);
    });
    
    // Initial render
    document.getElementById("true-content")!.textContent =
      content.myCondition(true);
    document.getElementById("false-content")!.textContent =
      content.myCondition(false);
    

    موارد إضافية

    لمزيد من المعلومات التفصيلية حول الإعداد والاستخدام، راجع الموارد التالية:

    تقدم هذه الموارد مزيدًا من الأفكار حول إعداد واستخدام Intlayer عبر بيئات وأطر عمل مختلفة.