שאלו את שאלתכם וקבלו סיכום של המסמך על ידי הפניה לדף הזה ולספק ה-AI שתבחרו
היסטוריית גרסאות
- "Update to Next.js >= 9.4.0 architecture"v9.4.022.8.2026
- "Update Solid useIntlayer API usage to direct property access"v8.9.04.5.2026
- "Initial history"v5.5.1029.6.2025
התוכן של הדף הזה תורגם באמצעות AI.
צפייה בגרסה האחרונה של התוכן המקורי באנגליתאם יש לכם רעיון לשיפור התיעוד הזה, אל תהססו לתרום על ידי הגשת Pull Request ב-GitHub.
קישור GitHub לתיעודהעתקת ה-Markdown של המסמך ללוח
Next.js Integration: useIntlayer Hook Documentation
The useIntlayer hook is tailored for Next.js applications to fetch and manage localized content efficiently. This documentation will focus on how to utilize the hook within Next.js projects, ensuring proper localization practices.
Importing useIntlayer in Next.js
Depending on whether you're working on client-side or server-side components in a Next.js application, you can import the useIntlayer hook as follows:
Client Component:
typescriptהעתקת קודהעתקת הקוד ללוח
Server Component:
tsxהעתקת קודהעתקת הקוד ללוח
Parameters
key: A string identifier for the dictionary key from which you want to retrieve content.locale(optional): A specific locale to use. If omitted, the hook defaults to the locale set in the client or server context.
Dictionary Files
It's crucial that all content keys are defined within content declaration files to prevent runtime errors and ensure type safety. This approach also facilitates TypeScript integration for compile-time validation.
Instructions for setting up content declaration files are available here.
Example Usage in Next.js
Here's how you can implement the useIntlayer hook within a Next.js page to dynamically load localized content based on the application's current locale:
העתקת הקוד ללוח
import { ClientComponentExample } from "@components/ClientComponentExample";
import { ServerComponentExample } from "@components/ServerComponentExample";
import { type NextPageIntlayer } from "next-intlayer";
import { useIntlayer } from "next-intlayer";
import { IntlayerProvider } from "next-intlayer/server";
const HomePage: NextPageIntlayer = async ({ params }) => {
const { locale } = await params;
const content = useIntlayer("homepage", locale);
return (
<IntlayerProvider locale={locale}>
<p>{content.introduction}</p>
<ClientComponentExample />
<ServerComponentExample />
</IntlayerProvider>
);
};
העתקת הקוד ללוח
import { ClientComponentExample } from "@components/ClientComponentExample";
import { ServerComponentExample } from "@components/ServerComponentExample";
import { type NextPageIntlayer, IntlayerClientProvider } from "next-intlayer";
import { useIntlayer, IntlayerServerProvider } from "next-intlayer/server";
const HomePage: NextPageIntlayer = async ({ params }) => {
const { locale } = await params;
const content = useIntlayer("homepage", locale);
return (
<>
<p>{content.introduction}</p>
<IntlayerClientProvider locale={locale}>
<ClientComponentExample />
</IntlayerClientProvider>
<IntlayerServerProvider locale={locale}>
<ServerComponentExample />
</IntlayerServerProvider>
</>
);
};
העתקת הקוד ללוח
"use-client";
import type { FC } from "react";
import { useIntlayer } from "next-intlayer";
const ClientComponentExample: FC = () => {
const content = useIntlayer("component-content");
return (
<div>
<h1>{content.title}</h1>
<p>{content.description}</p>
</div>
);
};
העתקת הקוד ללוח
import type { FC } from "react";
import { useIntlayer } from "next-intlayer/server";
const ServerComponentExample: FC = () => {
const content = useIntlayer("component-content");
return (
<div>
<h1>{content.title}</h1>
<p>{content.description}</p>
</div>
);
};
Handling Attribute Localization
To localize attributes such as alt, title, href, aria-label, etc., ensure you reference the content correctly:
העתקת הקוד ללוח
Further Information
- Intlayer Visual Editor: Learn how to use the visual editor for easier content management here.
This documentation outlines the use of the useIntlayer hook specifically within Next.js environments, providing a robust solution for managing localization across your Next.js applications.
