\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## Tài nguyên bổ sung\n\nĐể biết thêm thông tin chi tiết về cấu hình và cách sử dụng, hãy tham khảo các tài nguyên sau:\n\n- [Tài liệu CLI Intlayer](/vi/doc/concept/cli)\n- [Tài liệu React Intlayer](/vi/doc/environment/create-react-app)\n- [Tài liệu Next Intlayer](/vi/doc/environment/nextjs/15)\n\nNhững tài nguyên này cung cấp thêm những hiểu biết sâu sắc về cách thiết lập và sử dụng Intlayer trong các môi trường khác nhau và với các framework đa dạng.\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```\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
{{ ordinal(Math.abs(count) % 10)({ count }) }}
\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 );\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\nA common use case is displaying ordinal numbers (1st, 2nd, 3rd, etc.). You can combine `enu` with `insert` to create dynamic ordinal content:\n\n```typescript fileName=\"**/*.content.ts\" contentDeclarationFormat={[\"typescript\", \"esm\", \"commonjs\"]}\nimport { enu, insert, type Dictionary } from \"intlayer\";\n\nconst rankingContent = {\n key: \"ranking_component\",\n content: {\n ordinal: enu({\n 1: insert(\"{{count}}st place\"),\n 2: insert(\"{{count}}nd place\"),\n 3: insert(\"{{count}}rd place\"),\n fallback: insert(\"{{count}}th place\"),\n }),\n },\n} satisfies Dictionary;\n\nexport default rankingContent;\n```\n\n```json fileName=\"**/*.content.json\" contentDeclarationFormat=\"json\"\n{\n \"$schema\": \"https://intlayer.org/schema.json\",\n \"key\": \"ranking_component\",\n \"content\": {\n \"ordinal\": {\n \"nodeType\": \"enumeration\",\n \"enumeration\": {\n \"1\": {\n \"nodeType\": \"insertion\",\n \"insertion\": \"{{count}}st place\"\n },\n \"2\": {\n \"nodeType\": \"insertion\",\n \"insertion\": \"{{count}}nd place\"\n },\n \"3\": {\n \"nodeType\": \"insertion\",\n \"insertion\": \"{{count}}rd place\"\n },\n \"fallback\": {\n \"nodeType\": \"insertion\",\n \"insertion\": \"{{count}}th place\"\n }\n }\n }\n }\n}\n```\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```\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
{{ ordinal(Math.abs(count) % 10)({ count }) }}
\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 );\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## Additional Resources\n\nFor more detailed information on configuration and usage, refer to the following resources:\n\n- [Intlayer CLI Documentation](/vi/doc/concept/cli)\n- [React Intlayer Documentation](/vi/doc/environment/create-react-app)\n- [Next Intlayer Documentation](/vi/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":"Tìm hiểu cách khai báo và sử dụng các phép liệt kê trong trang web đa ngôn ngữ của bạn. Làm theo các bước trong tài liệu trực tuyến này để thiết lập dự án của bạn trong vài phút.","url":"https://intlayer.org/vi/doc/concept/content/enumeration","datePublished":"2025-08-23","dateModified":"2025-08-23","version":"5.5.10","keywords":"Liệt kê, Quốc tế hóa, Tài liệu, Intlayer, Next.js, JavaScript, React","license":"https://raw.githubusercontent.com/aymericzip/intlayer/refs/heads/main/LICENSE","audience":{"@type":"Audience","audienceType":"Nhà phát triển (Developers), Quản trị nội dung (Content Managers)"}}
Trong Intlayer, phép liệt kê được thực hiện thông qua hàm enu, hàm này ánh xạ các khóa cụ thể tới nội dung tương ứng của chúng. Các khóa này có thể đại diện cho các giá trị số, phạm vi, hoặc các định danh tùy chỉnh. Khi sử dụng với React Intlayer hoặc Next Intlayer, nội dung phù hợp sẽ được tự động chọn dựa trên locale của ứng dụng và các quy tắc đã định nghĩa.
Thiết lập Liệt kê
Để thiết lập phép liệt kê trong dự án Intlayer của bạn, bạn cần tạo một module nội dung bao gồm các định nghĩa liệt kê. Dưới đây là ví dụ về một phép liệt kê đơn giản cho số lượng xe hơi:
**/*.content.ts
Sao chép mã
Sao chép mã vào clipboard
import { enu, type Dictionary } from "intlayer";
const carEnumeration = {
key: "car_count",
content: {
numberOfCar: enu({
"<-1": "Ít hơn âm một chiếc xe",
"-1": "Âm một chiếc xe",
"0": "Không có xe",
"1": "Một chiếc xe",
">5": "Một vài chiếc xe",
">19": "Nhiều chiếc xe",
"fallback": "Giá trị dự phòng", // Tùy chọn
}),
},
} satisfies Dictionary;
export default carEnumeration;
Trong ví dụ này, enu ánh xạ các điều kiện khác nhau tới nội dung cụ thể. Khi được sử dụng trong một component React, Intlayer có thể tự động chọn nội dung phù hợp dựa trên biến được cung cấp.
Thứ tự khai báo rất quan trọng trong các phép liệt kê của Intlayer. Khai báo hợp lệ đầu tiên sẽ được chọn. Nếu có nhiều điều kiện áp dụng, hãy đảm bảo chúng được sắp xếp đúng để tránh hành vi không mong muốn.
Nếu không khai báo giá trị dự phòng, hàm sẽ trả về undefined nếu không có khóa nào khớp.
Sử dụng Enumeration với 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
Sao chép mã
Sao chép mã vào clipboard
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>
);
};
To use enumeration in Next.js Client Components, retrieve it via the useIntlayer hook. Here's an example:
**/*.tsx
Sao chép mã
Sao chép mã vào clipboard
"use client";
import type { FC } from "react";
import { useIntlayer } from "next-intlayer";
const CarComponent: FC = () => {
const { numberOfCar } = useIntlayer("car_count");
return (
<div>
<p>{numberOfCar(6)}</p>
</div>
);
};
export default CarComponent;
To use enumeration in Vue components, retrieve it via the useIntlayer hook. Here's an example:
Những tài nguyên này cung cấp thêm những hiểu biết sâu sắc về cách thiết lập và sử dụng Intlayer trong các môi trường khác nhau và với các framework đa dạng.
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
Sao chép mã
Sao chép mã vào clipboard
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>
);
};
To 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:
To 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:
To 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:
**/*.svelte
Sao chép mã
Sao chép mã vào clipboard
<script lang="ts">import { useIntlayer } from "svelte-intlayer";export let count: number;const content = useIntlayer("ranking_component");$: lastDigit = Math.abs(count) % 10;</script><div><p>{$content.ordinal(lastDigit)({ count })}</p></div>
To 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:
**/*.tsx
Sao chép mã
Sao chép mã vào clipboard
import type { FC } from "preact";
import { useIntlayer } from "preact-intlayer";
const RankingComponent: FC<{ count: number }> = ({ count }) => {
const { ordinal } = useIntlayer("ranking_component");
const lastDigit = Math.abs(count) % 10;
return (
<div>
<p>{ordinal(lastDigit)({ count })}</p>
</div>
);
};
export default RankingComponent;
To 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:
To 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:
To 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:
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
Sao chép mã
Sao chép mã vào clipboard
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>
);
};
To 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:
To 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:
To 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:
**/*.svelte
Sao chép mã
Sao chép mã vào clipboard
<script lang="ts">import { useIntlayer } from "svelte-intlayer";export let count: number;const content = useIntlayer("ranking_component");$: lastDigit = Math.abs(count) % 10;</script><div><p>{$content.ordinal(lastDigit)({ count })}</p></div>
To 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:
**/*.tsx
Sao chép mã
Sao chép mã vào clipboard
import type { FC } from "preact";
import { useIntlayer } from "preact-intlayer";
const RankingComponent: FC<{ count: number }> = ({ count }) => {
const { ordinal } = useIntlayer("ranking_component");
const lastDigit = Math.abs(count) % 10;
return (
<div>
<p>{ordinal(lastDigit)({ count })}</p>
</div>
);
};
export default RankingComponent;
To 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:
To 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:
To 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: