\n\n \n ```\n\n \n \n ```svelte fileName=\"Hero.svelte\" contentDeclarationFormat={[\"typescript\", \"esm\", \"commonjs\"]}\n \n\n
\n

{$content.headline}

\n {$content.cta}\n
\n ```\n\n
\n \n ```tsx fileName=\"Hero.tsx\" contentDeclarationFormat={[\"typescript\", \"esm\", \"commonjs\"]}\n import { useIntlayer } from \"preact-intlayer\";\n\n export const Hero = () => {\n const { headline, cta } = useIntlayer(\"hero-banner\");\n // → 默认变体\n\n return (\n
\n

{headline}

\n {cta}\n
\n );\n };\n ```\n\n
\n \n ```tsx fileName=\"Hero.tsx\" contentDeclarationFormat={[\"typescript\", \"esm\", \"commonjs\"]}\n import { useIntlayer } from \"solid-intlayer\";\n\n export const Hero = () => {\n const content = useIntlayer(\"hero-banner\");\n // → 默认变体\n\n return (\n
\n

{content().headline}

\n {content().cta}\n
\n );\n };\n ```\n\n
\n \n ```typescript fileName=\"hero.component.ts\" contentDeclarationFormat={[\"typescript\", \"esm\", \"commonjs\"]}\n import { Component } from \"@angular/core\";\n import { useIntlayer } from \"angular-intlayer\";\n\n @Component({\n selector: \"app-hero\",\n template: `\n
\n

{{ content().headline }}

\n {{ content().cta }}\n
\n `,\n })\n export class HeroComponent {\n content = useIntlayer(\"hero-banner\");\n }\n ```\n\n
\n \n ```javascript fileName=\"hero.js\"\n import { useIntlayer } from \"vanilla-intlayer\";\n\n const { headline, cta } = useIntlayer(\"hero-banner\");\n\n document.body.innerHTML = `\n
\n

${headline}

\n ${cta}\n
\n `;\n ```\n\n
\n\n\n### 命名变体\n\n```tsx\nconst { headline, cta } = useIntlayer(\"hero-banner\", {\n variant: \"black_friday\",\n});\n```\n\n### 使用显式语言环境的命名变体\n\n```tsx\nconst content = useIntlayer(\"hero-banner\", {\n variant: \"black_friday\",\n locale: \"zh\",\n});\n```\n\n## 典型使用场景\n\n- 由实验键值驱动的文案 A/B 测试\n- 季节性或促销横幅\n- 受功能标志(feature flags)控制的消息文案\n- 针对特定区域的营销活动\n","description":"在 Intlayer 内容文件中使用 variant 元数据字段来声明命名内容替代方案(A/B 测试、季节性横幅、受功能标志控制的文案),并在运行时切换它们而无需更改代码。","url":"https://intlayer.org/zh/doc/concept/variants","datePublished":"2026-06-12","dateModified":"2026-06-12","version":"9.0.0","keywords":"变体, A/B 测试, 功能标志, 动态内容, Intlayer, 国际化","license":"https://raw.githubusercontent.com/aymericzip/intlayer/refs/heads/main/LICENSE","audience":{"@type":"Audience","audienceType":"开发者,内容经理"}}
    作者:
    Creation:2026-06-12Last update:2026-06-12

    变体

    变体(Variant)是一组共享相同字典键(key)但声明不同变体名称(variant)的内容文件。Intlayer 会根据传递给 useIntlayer 的选择器提供合适的文件。

    声明变体

    每个文件代表一个命名替代方案。如果省略 variant(或将其设置为 "default"),则表示它是默认(回退)变体。

    hero-banner.content.ts
    import { t, type Dictionary } from "intlayer";
    
    const dictionary = {
      key: "hero-banner",
      variant: "default",
      content: {
        headline: t({
          en: "Build faster with Intlayer",
          fr: "Développez plus vite avec Intlayer",
        }),
        cta: t({ en: "Get started", fr: "Commencer" }),
      },
    } satisfies Dictionary;
    
    export default dictionary;
    hero-banner.black-friday.content.ts
    import { t, type Dictionary } from "intlayer";
    
    const dictionary = {
      key: "hero-banner",
      variant: "black_friday",
      content: {
        headline: t({
          en: "50 % off — today only",
          fr: "−50 % — aujourd'hui seulement",
        }),
        cta: t({ en: "Shop now", fr: "Acheter maintenant" }),
      },
    } satisfies Dictionary;
    
    export default dictionary;

    使用变体

    默认变体

    Hero.tsx
    import { useIntlayer } from "react-intlayer";
    
    export const Hero = () => {
      const { headline, cta } = useIntlayer("hero-banner");
      // → 默认变体
    
      return (
        <section>
          <h1>{headline}</h1>
          <a>{cta}</a>
        </section>
      );
    };

    命名变体

    tsx
    const { headline, cta } = useIntlayer("hero-banner", {  variant: "black_friday",});

    使用显式语言环境的命名变体

    tsx
    const content = useIntlayer("hero-banner", {  variant: "black_friday",  locale: "zh",});

    典型使用场景

    • 由实验键值驱动的文案 A/B 测试
    • 季节性或促销横幅
    • 受功能标志(feature flags)控制的消息文案
    • 针对特定区域的营销活动