Đặ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: useLocale Hook Documentation
This section provides comprehensive details on the useLocale hook from the react-intlayer library, designed for handling locale management in React applications.
Importing useLocale in React
To integrate the useLocale hook into your React application, import it from its respective package:
import { useLocale } from "react-intlayer"; // Used in React components for locale managementOverview
The useLocale hook offers an easy way to access and manipulate the locale settings within React components. It provides access to the current locale, the default locale, all available locales, and functions to update the locale settings.
Usage
Here’s how you can use the useLocale hook within a React component:
Sao chép đoạn mã vào khay nhớ tạm (clipboard)
import type { FC } from "react";import { useLocale } from "react-intlayer";const LocaleSwitcher: FC = () => { const { locale, defaultLocale, availableLocales, setLocale } = useLocale(); return ( <div> <h1>Current Locale: {locale}</h1> <p>Default Locale: {defaultLocale}</p> <select value={locale} onChange={(e) => setLocale(e.target.value)}> {availableLocales.map((loc) => ( <option key={loc} value={loc}> {loc} </option> ))} </select> </div> );};export default LocaleSwitcher;Parameters and Return Values
When you invoke the useLocale hook, it returns an object containing the following properties:
- locale: The current locale as set in the React context.
- defaultLocale: The primary locale defined in the configuration.
- availableLocales: A list of all locales available as defined in the configuration.
- setLocale: A function to update the current locale within the application's context.
Example
This example shows a component that uses the useLocale hook to render a locale switcher, allowing users to dynamically change the locale of the application:
Sao chép đoạn mã vào khay nhớ tạm (clipboard)
import type { FC } from "react";import { useLocale } from "react-intlayer";const LocaleSelector: FC = () => { const { locale, setLocale, availableLocales } = useLocale(); const handleLocaleChange = (newLocale) => { setLocale(newLocale); }; return ( <select value={locale} onChange={(e) => handleLocaleChange(e.target.value)}> {availableLocales.map((locale) => ( <option key={locale} value={locale}> {locale} </option> ))} </select> );};Conclusion
The useLocale hook from react-intlayer is an essential tool for managing locales in your React applications, providing the functionality needed to adapt your application to various international audiences effectively.