Đặ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
Lịch sử phiên bản
- "Khởi tạo lịch sử"v5.5.1029/6/2025
Nộ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
Dịch thuật
Định nghĩa Dịch thuật
Hàm t trong intlayer cho phép bạn khai báo nội dung bằng nhiều ngôn ngữ. Hàm này đảm bảo an toàn kiểu, báo lỗi nếu thiếu bất kỳ bản dịch nào, điều này đặc biệt hữu ích trong môi trường TypeScript.
Dưới đây là ví dụ về cách khai báo nội dung với các bản dịch.
Sao chép mã vào clipboard
import { t, type Dictionary } from "intlayer";
interface Content {
welcomeMessage: string; // thông điệp chào mừng
}
export default {
key: "multi_lang",
content: {
welcomeMessage: t({
en: "Welcome to our application",
fr: "Bienvenue dans notre application",
es: "Bienvenido a nuestra aplicación",
}),
},
} satisfies Dictionary<Content>;
Cấu hình cho các Ngôn ngữ (Locales)
Để đảm bảo xử lý dịch thuật đúng cách, bạn có thể cấu hình các ngôn ngữ được chấp nhận trong intlayer.config.ts. Cấu hình này cho phép bạn định nghĩa các ngôn ngữ mà ứng dụng của bạn hỗ trợ:
Sao chép mã vào clipboard
import { Locales, type IntlayerConfig } from "intlayer";
const config: IntlayerConfig = {
internationalization: {
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
},
};
export default config;
Sử dụng Dịch thuật trong Các Thành phần React
With react-intlayer, you can use translations in React components. Here's an example:
Sao chép mã vào clipboard
import type { FC } from "react";
import { useIntlayer } from "react-intlayer";
const MyComponent: FC = () => {
const content = useIntlayer("multi_lang");
return (
<div>
<p>{content.welcomeMessage}</p>
</div>
);
};
export default MyComponent;
This component fetches the corresponding translation based on the current locale set in your application.
With next-intlayer, you can use translations in React Server Components or Client Components. Here's an example in a Client Component:
Sao chép mã vào clipboard
"use client";
import type { FC } from "react";
import { useIntlayer } from "next-intlayer";
const MyComponent: FC = () => {
const content = useIntlayer("multi_lang");
return (
<div>
<p>{content.welcomeMessage}</p>
</div>
);
};
export default MyComponent;
With vue-intlayer, you can use translations in Vue components. Here's an example:
Sao chép mã vào clipboard
With svelte-intlayer, you can use translations in Svelte components. The hook returns a Svelte store. Here's an example:
Sao chép mã vào clipboard
With preact-intlayer, you can use translations in Preact components. Here's an example:
Sao chép mã vào clipboard
import type { FC } from "preact";
import { useIntlayer } from "preact-intlayer";
const MyComponent: FC = () => {
const content = useIntlayer("multi_lang");
return (
<div>
<p>{content.welcomeMessage}</p>
</div>
);
};
export default MyComponent;
With solid-intlayer, you can use translations in SolidJS components. Here's an example:
Sao chép mã vào clipboard
import type { Component } from "solid-js";
import { useIntlayer } from "solid-intlayer";
const MyComponent: Component = () => {
const content = useIntlayer("multi_lang");
return (
<div>
<p>{content.welcomeMessage}</p>
</div>
);
};
export default MyComponent;
With angular-intlayer, you can use translations in Angular components. Here's an example:
Sao chép mã vào clipboard
With vanilla-intlayer, you can use translations by subscribing to content changes. Here's an example:
Sao chép mã vào clipboard
import { installIntlayer, useIntlayer } from "vanilla-intlayer";
installIntlayer();
const content = useIntlayer("multi_lang").onChange((newContent) => {
document.getElementById("welcome-message")!.textContent = String(
newContent.welcomeMessage
);
});
// Initial render
document.getElementById("welcome-message")!.textContent = String(
content.welcomeMessage
);
