\n\n\n```\n\n \n \n\nTo use enumeration in Svelte components, retrieve it via the `useIntlayer` hook. The store is accessed with `$`. Here's an example:\n\n```svelte fileName=\"**/*.svelte\"\n\n\n
\n

{$content.numberOfCar(6)}

\n
\n```\n\n
\n \n\nTo use enumeration in Preact components, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```tsx fileName=\"**/*.tsx\" codeFormat={[\"typescript\", \"esm\"]}\nimport type { FC } from \"preact\";\nimport { useIntlayer } from \"preact-intlayer\";\n\nconst CarComponent: FC = () => {\n const { numberOfCar } = useIntlayer(\"car_count\");\n\n return (\n
\n

{numberOfCar(6)}

\n
\n );\n};\n\nexport default CarComponent;\n```\n\n
\n \n\nTo use enumeration in SolidJS components, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```tsx fileName=\"**/*.tsx\" codeFormat={[\"typescript\", \"esm\"]}\nimport type { Component } from \"solid-js\";\nimport { useIntlayer } from \"solid-intlayer\";\n\nconst CarComponent: Component = () => {\n const { numberOfCar } = useIntlayer(\"car_count\");\n\n return (\n
\n

{numberOfCar(6)}

\n
\n );\n};\n\nexport default CarComponent;\n```\n\n
\n \n\nTo use enumeration in Angular components, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```typescript fileName=\"app.component.ts\" codeFormat=\"typescript\"\nimport { Component } from \"@angular/core\";\nimport { useIntlayer } from \"angular-intlayer\";\n\n@Component({\n selector: \"app-car\",\n template: `\n
\n

{{ content().numberOfCar(6) }}

\n
\n `,\n})\nexport class CarComponent {\n content = useIntlayer(\"car_count\");\n}\n```\n\n
\n \n\nTo use enumeration with `vanilla-intlayer`, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```typescript fileName=\"**/*.ts\" codeFormat={[\"typescript\", \"esm\"]}\nimport { installIntlayer, useIntlayer } from \"vanilla-intlayer\";\n\ninstallIntlayer();\n\nconst content = useIntlayer(\"car_count\").onChange((newContent) => {\n document.getElementById(\"cars\")!.textContent = newContent.numberOfCar(6);\n});\n\n// Initial render\ndocument.getElementById(\"cars\")!.textContent = content.numberOfCar(6);\n```\n\n \n\n\n## 附加资源\n\n有关配置和使用的更详细信息,请参阅以下资源:\n\n- [Intlayer CLI 文档](/zh/doc/concept/cli)\n- [React Intlayer 文档](/zh/doc/environment/create-react-app)\n- [Next Intlayer 文档](/zh/doc/environment/nextjs/15)\n\n这些资源提供了关于在不同环境和各种框架中设置和使用 Intlayer 的更多见解。\n\n### Using Ordinal Enumeration\n\n\n \n\nTo use this in a React component, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:\n\n```tsx fileName=\"**/*.tsx\" codeFormat={[\"typescript\", \"esm\"]}\nimport type { FC } from \"react\";\nimport { useIntlayer } from \"react-intlayer\";\n\nconst RankingComponent: FC<{ count: number }> = ({ count }) => {\n const { ordinal } = useIntlayer(\"ranking_component\");\n\n // Get the last digit to determine the correct suffix\n const lastDigit = Math.abs(count) % 10;\n\n return (\n
\n

\n {\n ordinal(lastDigit)({ count }) // e.g., \"5th place\" for count=5\n }\n

\n
\n );\n};\n```\n\n
\n \n\nTo use this in Next.js Client Components, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:\n\n```tsx fileName=\"**/*.tsx\" codeFormat={[\"typescript\", \"esm\"]}\n\"use client\";\n\nimport type { FC } from \"react\";\nimport { useIntlayer } from \"next-intlayer\";\n\nconst RankingComponent: FC<{ count: number }> = ({ count }) => {\n const { ordinal } = useIntlayer(\"ranking_component\");\n const lastDigit = Math.abs(count) % 10;\n\n return (\n
\n

{ordinal(lastDigit)({ count })}

\n
\n );\n};\n\nexport default RankingComponent;\n```\n\n
\n \n\nTo use this in Vue components, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:\n\n```vue fileName=\"**/*.vue\"\n\n\n\n```\n\n \n \n\nTo use this in Svelte components, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:\n\n```svelte fileName=\"**/*.svelte\"\n\n\n
\n

{$content.ordinal(lastDigit)({ count })}

\n
\n```\n\n
\n \n\nTo use this in Preact components, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:\n\n```tsx fileName=\"**/*.tsx\" codeFormat={[\"typescript\", \"esm\"]}\nimport type { FC } from \"preact\";\nimport { useIntlayer } from \"preact-intlayer\";\n\nconst RankingComponent: FC<{ count: number }> = ({ count }) => {\n const { ordinal } = useIntlayer(\"ranking_component\");\n const lastDigit = Math.abs(count) % 10;\n\n return (\n
\n

{ordinal(lastDigit)({ count })}

\n
\n );\n};\n\nexport default RankingComponent;\n```\n\n
\n \n\nTo use this in SolidJS components, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:\n\n```tsx fileName=\"**/*.tsx\" codeFormat={[\"typescript\", \"esm\"]}\nimport type { Component } from \"solid-js\";\nimport { useIntlayer } from \"solid-intlayer\";\n\nconst RankingComponent: Component<{ count: number }> = (props) => {\n const { ordinal } = useIntlayer(\"ranking_component\");\n\n return (\n
\n

{ordinal(Math.abs(props.count) % 10)({ count: props.count })}

\n
\n );\n};\n\nexport default RankingComponent;\n```\n\n
\n \n\nTo use this in Angular components, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:\n\n```typescript fileName=\"app.component.ts\" codeFormat=\"typescript\"\nimport { Component, Input } from \"@angular/core\";\nimport { useIntlayer } from \"angular-intlayer\";\n\n@Component({\n selector: \"app-ranking\",\n template: `\n
\n

{{ content().ordinal(lastDigit())({ count }) }}

\n
\n `,\n})\nexport class RankingComponent {\n @Input() count!: number;\n\n content = useIntlayer(\"ranking_component\");\n\n lastDigit() {\n return Math.abs(this.count) % 10;\n }\n}\n```\n\n
\n \n\nTo use this with `vanilla-intlayer`, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:\n\n```typescript fileName=\"**/*.ts\" codeFormat={[\"typescript\", \"esm\"]}\nimport { installIntlayer, useIntlayer } from \"vanilla-intlayer\";\n\ninstallIntlayer();\n\nconst content = useIntlayer(\"ranking_component\");\nconst lastDigit = Math.abs(5) % 10;\n\ndocument.getElementById(\"ranking\")!.textContent = content.ordinal(lastDigit)({\n count: 5,\n});\n```\n\n \n
\n\n## Combining Enumeration with Insert for Ordinal Numbers\n\n\n \n\nTo use enumeration in a React component, you can leverage the `useIntlayer` hook from the `react-intlayer` package. This hook retrieves the correct content based on the specified ID. Here's an example of how to use it:\n\n```tsx fileName=\"**/*.tsx\" codeFormat={[\"typescript\", \"esm\"]}\nimport type { FC } from \"react\";\nimport { useIntlayer } from \"react-intlayer\";\n\nconst CarComponent: FC = () => {\n const { numberOfCar } = useIntlayer(\"car_count\");\n\n return (\n
\n

\n {\n numberOfCar(0) // Output: No cars\n }\n

\n

\n {\n numberOfCar(6) // Output: Some cars\n }\n

\n

\n {\n numberOfCar(20) // Output: Many cars\n }\n

\n

\n {\n numberOfCar(0.01) // Output: Fallback value\n }\n

\n
\n );\n};\n```\n\n
\n \n\nTo use enumeration in Next.js Client Components, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```tsx fileName=\"**/*.tsx\" codeFormat={[\"typescript\", \"esm\"]}\n\"use client\";\n\nimport type { FC } from \"react\";\nimport { useIntlayer } from \"next-intlayer\";\n\nconst CarComponent: FC = () => {\n const { numberOfCar } = useIntlayer(\"car_count\");\n\n return (\n
\n

{numberOfCar(6)}

\n
\n );\n};\n\nexport default CarComponent;\n```\n\n
\n \n\nTo use enumeration in Vue components, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```vue fileName=\"**/*.vue\"\n\n\n\n```\n\n \n \n\nTo use enumeration in Svelte components, retrieve it via the `useIntlayer` hook. The store is accessed with `$`. Here's an example:\n\n```svelte fileName=\"**/*.svelte\"\n\n\n
\n

{$content.numberOfCar(6)}

\n
\n```\n\n
\n \n\nTo use enumeration in Preact components, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```tsx fileName=\"**/*.tsx\" codeFormat={[\"typescript\", \"esm\"]}\nimport type { FC } from \"preact\";\nimport { useIntlayer } from \"preact-intlayer\";\n\nconst CarComponent: FC = () => {\n const { numberOfCar } = useIntlayer(\"car_count\");\n\n return (\n
\n

{numberOfCar(6)}

\n
\n );\n};\n\nexport default CarComponent;\n```\n\n
\n \n\nTo use enumeration in SolidJS components, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```tsx fileName=\"**/*.tsx\" codeFormat={[\"typescript\", \"esm\"]}\nimport type { Component } from \"solid-js\";\nimport { useIntlayer } from \"solid-intlayer\";\n\nconst CarComponent: Component = () => {\n const { numberOfCar } = useIntlayer(\"car_count\");\n\n return (\n
\n

{numberOfCar(6)}

\n
\n );\n};\n\nexport default CarComponent;\n```\n\n
\n \n\nTo use enumeration in Angular components, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```typescript fileName=\"app.component.ts\" codeFormat=\"typescript\"\nimport { Component } from \"@angular/core\";\nimport { useIntlayer } from \"angular-intlayer\";\n\n@Component({\n selector: \"app-car\",\n template: `\n
\n

{{ content().numberOfCar(6) }}

\n
\n `,\n})\nexport class CarComponent {\n content = useIntlayer(\"car_count\");\n}\n```\n\n
\n \n\nTo use enumeration with `vanilla-intlayer`, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```typescript fileName=\"**/*.ts\" codeFormat={[\"typescript\", \"esm\"]}\nimport { installIntlayer, useIntlayer } from \"vanilla-intlayer\";\n\ninstallIntlayer();\n\nconst content = useIntlayer(\"car_count\").onChange((newContent) => {\n document.getElementById(\"cars\")!.textContent = newContent.numberOfCar(6);\n});\n\n// Initial render\ndocument.getElementById(\"cars\")!.textContent = content.numberOfCar(6);\n```\n\n \n
\n## Additional Resources\n\nFor more detailed information on configuration and usage, refer to the following resources:\n\n- [Intlayer CLI Documentation](/zh/doc/concept/cli)\n- [React Intlayer Documentation](/zh/doc/environment/create-react-app)\n- [Next Intlayer Documentation](/zh/doc/environment/nextjs/15)\n\nThese resources provide further insights into the setup and usage of Intlayer in different environments and with various frameworks.\n","description":"了解如何在您的多语言网站中声明和使用枚举。按照本在线文档中的步骤,几分钟内即可设置您的项目。","url":"https://intlayer.org/zh/doc/concept/content/enumeration","datePublished":"2024-08-11","dateModified":"2025-06-29","version":"5.5.10","keywords":"枚举, 国际化, 文档, Intlayer, Next.js, JavaScript, React","license":"https://raw.githubusercontent.com/aymericzip/intlayer/refs/heads/main/LICENSE","audience":{"@type":"Audience","audienceType":"开发者,内容经理"}}
    作者:
    Creation:2024-08-11Last update:2025-06-29

    枚举 / 复数形式

    枚举的工作原理

    在 Intlayer 中,枚举是通过 enu 函数实现的,该函数将特定的键映射到对应的内容。这些键可以表示数值、范围或自定义标识符。当与 React Intlayer 或 Next Intlayer 一起使用时,会根据应用程序的语言环境和定义的规则自动选择合适的内容。

    设置枚举

    要在您的 Intlayer 项目中设置枚举,您需要创建一个包含枚举定义的内容模块。以下是一个关于汽车数量的简单枚举示例:

    **/*.content.ts
    import { enu, type Dictionary } from "intlayer";
    
    const carEnumeration = {
      key: "car_count",
      content: {
        numberOfCar: enu({
          "<-1": "少于负一辆车",
          "-1": "负一辆车",
          "0": "没有车",
          "1": "一辆车",
          ">5": "几辆车",
          ">19": "许多车",
          "fallback": "备用值", // 可选
        }),
      },
    } satisfies Dictionary;
    
    export default carEnumeration;

    在此示例中,enu 将各种条件映射到特定内容。当在 React 组件中使用时,Intlayer 可以根据给定的变量自动选择合适的内容。

    在 Intlayer 枚举中,声明的顺序非常重要。第一个有效的声明将被选中。如果多个条件适用,请确保它们的顺序正确,以避免意外行为。
    如果未声明备用值,当没有匹配的键时,函数将返回 undefined

    在 React Intlayer 中使用枚举

    To use enumeration in a React component, you can leverage the useIntlayer hook from the react-intlayer package. This hook retrieves the correct content based on the specified ID. Here's an example of how to use it:

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const CarComponent: FC = () => {
    const { numberOfCar } = useIntlayer("car_count");
    
    return (
      <div>
        <p>
          {
            numberOfCar(0) // Output: No cars
          }
        </p>
        <p>
          {
            numberOfCar(6) // Output: Some cars
          }
        </p>
        <p>
          {
            numberOfCar(20) // Output: Many cars
          }
        </p>
        <p>
          {
            numberOfCar(0.01) // Output: Fallback value
          }
        </p>
      </div>
    );
    };

    附加资源

    有关配置和使用的更详细信息,请参阅以下资源:

    这些资源提供了关于在不同环境和各种框架中设置和使用 Intlayer 的更多见解。

    Using Ordinal Enumeration

    To use this in a React component, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const RankingComponent: FC<{ count: number }> = ({ count }) => {
    const { ordinal } = useIntlayer("ranking_component");
    
    // Get the last digit to determine the correct suffix
    const lastDigit = Math.abs(count) % 10;
    
    return (
      <div>
        <p>
          {
            ordinal(lastDigit)({ count }) // e.g., "5th place" for count=5
          }
        </p>
      </div>
    );
    };

    Combining Enumeration with Insert for Ordinal Numbers

    To use enumeration in a React component, you can leverage the useIntlayer hook from the react-intlayer package. This hook retrieves the correct content based on the specified ID. Here's an example of how to use it:

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const CarComponent: FC = () => {
    const { numberOfCar } = useIntlayer("car_count");
    
    return (
      <div>
        <p>
          {
            numberOfCar(0) // Output: No cars
          }
        </p>
        <p>
          {
            numberOfCar(6) // Output: Some cars
          }
        </p>
        <p>
          {
            numberOfCar(20) // Output: Many cars
          }
        </p>
        <p>
          {
            numberOfCar(0.01) // Output: Fallback value
          }
        </p>
      </div>
    );
    };

    Additional Resources

    For more detailed information on configuration and usage, refer to the following resources:

    These resources provide further insights into the setup and usage of Intlayer in different environments and with various frameworks.