Đặ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 ServerLịch sử phiên bản
- Init historyv5.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
React Integration: useIntlayer Hook Documentation
This section provides detailed guidance on using the useIntlayer hook within React applications, allowing for efficient content localization.
Importing useIntlayer in React
The useIntlayer hook can be integrated into React applications by importing it based on the context:
Client Component:
import { useIntlayer } from "react-intlayer"; // Used in client-side React componentsServer Component:
Parameters
The hook accepts two parameters:
- key: The dictionary key to retrieve localized content.
- locale (optional): The desired locale. Defaults to the context's locale if not specified.
Dictionary
All dictionary keys must be declared within content declaration files to enhance type safety and avoid errors. You can find the setup instructions here.
Example Usage in React
Demonstrating the useIntlayer hook within a React component:
Sao chép đoạn mã vào khay nhớ tạm (clipboard)
import type { FC } from "react";import { ClientComponentExample, ServerComponentExample } from "@components";import { IntlayerProvider } from "react-intlayer";import { useIntlayer, IntlayerServerProvider } from "react-intlayer/server";import { Locales } from "intlayer";const App: FC<{ locale: Locales }> = ({ locale }) => { const content = useIntlayer("homepage", locale); return ( <> <p>{content.introduction}</p> <IntlayerProvider locale={locale}> <ClientComponentExample /> </IntlayerProvider> <IntlayerServerProvider locale={locale}> <ServerComponentExample /> </IntlayerServerProvider> </> );};Sao chép đoạn mã vào khay nhớ tạm (clipboard)
import type { FC } from "react";import { useIntlayer } from "react-intlayer";const ComponentExample: FC = () => { const content = useIntlayer("component-example"); return ( <div> <h1>{content.title}</h1> <p>{content.description}</p> </div> );};Sao chép đoạn mã vào khay nhớ tạm (clipboard)
import { useIntlayer } from "react-intlayer/server";const ServerComponentExample = () => { const content = useIntlayer("server-component"); return ( <div> <h1>{content.title}</h1> <p>{content.description}</p> </div> );};Handling Attributes
When localizing attributes, access the content values appropriately:
<button title={content.buttonTitle.value}>{content.buttonText}</button>Additional Resources
- Intlayer Visual Editor: For a more intuitive content management experience, refer to the visual editor documentation here.
This section specifically targets the integration of the useIntlayer hook in React applications, simplifying the localization process and ensuring content consistency across different locales.