Đặt câu hỏi và nhận tóm tắt tài liệu bằng cách tham chiếu trang này và nhà cung cấp AI bạn chọn
Bằng cách tích hợp Intlayer MCP Server vào trợ lý AI ưa thích của bạn, bạn có thể truy xuất toàn bộ tài liệu trực tiếp từ ChatGPT, DeepSeek, Cursor, VSCode, v.v.
Xem tài liệu MCP ServerNội dung của trang này đã được dịch bằng AI.
Xem phiên bản mới nhất của nội dung gốc bằng tiếng AnhNếu bạn có ý tưởng để cải thiện tài liệu này, vui lòng đóng góp bằng cách gửi pull request trên GitHub.
Liên kết GitHub tới tài liệuSao chép Markdown của tài liệu vào bộ nhớ tạm
Declaration of Per-Locale Content Declaration in Intlayer
Intlayer supports two ways to declare multilingual content:
- Single file with all translations
- One file per locale (per-locale format)
This flexibility enables:
- Easy migration from other i18n tools
- Support for automated translation workflows
- Clear organization of translations into separate, locale-specific files
Single File with Multiple Translations
This format is ideal for:
- Quick iteration in code.
- Seamless integration with the CMS.
This is the recommended approach for most use cases. It centralizes translations, making it easy to iterate and integrate with the CMS.
Sao chép đoạn mã vào khay nhớ tạm (clipboard)
import { t, type Dictionary } from "intlayer";const helloWorldContent = { key: "hello-world", content: { multilingualContent: t({ en: "Title of my component", es: "Título de mi componente", }), },} satisfies Dictionary;export default helloWorldContent;Recommended: This format is best when using Intlayer's visual editor or managing translations directly in the code.
Per-Locale Format
This format is useful when:
- You want to version or override translations independently.
- You're integrating machine or human translation workflows.
You can also split translations into individual locale files by specifying the locale field:
Sao chép đoạn mã vào khay nhớ tạm (clipboard)
import { t, Locales, type Dictionary } from "intlayer";const helloWorldContent = { key: "hello-world", locale: Locales.ENGLISH, // Important content: { multilingualContent: "Title of my component" },} satisfies Dictionary;export default helloWorldContent;Sao chép đoạn mã vào khay nhớ tạm (clipboard)
import { t, Locales, type Dictionary } from "intlayer";const helloWorldContent = { key: "hello-world", locale: Locales.SPANISH, // Important content: { multilingualContent: "Título de mi componente" },} satisfies Dictionary;export default helloWorldContent;Important: Make sure the locale field is defined. It tells Intlayer which language the file represents.
Note: In both cases, the content declaration file must follow the naming pattern *.content.{ts,tsx,js,jsx,mjs,cjs,json} to be recognized by Intlayer. The .[locale] suffix is optional and used only as a naming convention.
Mixing Formats
You can combine both declaration approaches for the same content key. For example:
- Declare your base content statically in a file like index.content.ts.
- Add or override specific translations in separate files such as index.fr.content.ts or index.content.json.
This setup is especially useful when:
- You want to define the initial content structure in code.
- You plan to enrich or complete translations later using the CMS or automated tools.
.└── Components └── MyComponent ├── index.content.ts ├── index.content.json └── index.tsExample
Here a multilingual content declaration file:
Sao chép đoạn mã vào khay nhớ tạm (clipboard)
import { t, type Dictionary } from "intlayer";const helloWorldContent = { key: "hello-world", locale: Locales.ENGLISH, content: { multilingualContent: "Title of my component", projectName: "My project", },} satisfies Dictionary;export default helloWorldContent;Sao chép đoạn mã vào khay nhớ tạm (clipboard)
{ "$schema": "https://intlayer.org/schema.json", "key": "hello-world", "content": { "multilingualContent": { "nodeType": "translation", "translation": { "fr": "Titre de mon composant", "es": "Título de mi componente" } } }}Intlayer merges multilingual and per-locale files automatically.
Sao chép đoạn mã vào khay nhớ tạm (clipboard)
import { getIntlayer, Locales } from "intlayer";const intlayer = getIntlayer("hello-world"); // Default locale is ENGLISH, so it will return the ENGLISH contentconsole.log(JSON.stringify(intlayer, null, 2));// Result:// {// "multilingualContent": "Title of my component",// "projectName": "My project"// }const intlayer = getIntlayer("hello-world", Locales.SPANISH);console.log(JSON.stringify(intlayer, null, 2));// Result:// {// "multilingualContent": "Título de mi componente",// "projectName": "My project"// }const intlayer = getIntlayer("hello-world", Locales.FRENCH);console.log(JSON.stringify(intlayer, null, 2));// Result:// {// "multilingualContent": "Titre de mon composant",// "projectName": "My project"// }Automatic Translation Generation
Use the intlayer CLI to auto-fill missing translations based on your preferred services.