שאלו את שאלתכם וקבלו סיכום של המסמך על ידי הפניה לדף הזה ולספק ה-AI שתבחרו
היסטוריית גרסאות
- "Init history"v5.5.1029.6.2025
התוכן של הדף הזה תורגם באמצעות AI.
צפייה בגרסה האחרונה של התוכן המקורי באנגליתאם יש לכם רעיון לשיפור התיעוד הזה, אל תהססו לתרום על ידי הגשת Pull Request ב-GitHub.
קישור GitHub לתיעודהעתקת ה-Markdown של המסמך ללוח
Translation
Defining Translations
The t function in intlayer allows you to declare content in multiple languages. This function ensures type safety, raising an error if any translations are missing, which is particularly useful in TypeScript environments.
Here's an example of how to declare content with translations.
העתקת הקוד ללוח
import { t, type Dictionary } from "intlayer";
interface Content {
welcomeMessage: string;
}
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>;
Configuration for Locales
To ensure proper translation handling, you can configure the accepted locales in intlayer.config.ts. This configuration allows you to define the languages that your application supports:
העתקת הקוד ללוח
import { Locales, type IntlayerConfig } from "intlayer";
const config: IntlayerConfig = {
internationalization: {
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
},
};
export default config;
Using Translations
With react-intlayer, you can use translations in React components. Here's an example:
העתקת הקוד ללוח
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:
העתקת הקוד ללוח
"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:
העתקת הקוד ללוח
With svelte-intlayer, you can use translations in Svelte components. The hook returns a Svelte store. Here's an example:
העתקת הקוד ללוח
With preact-intlayer, you can use translations in Preact components. Here's an example:
העתקת הקוד ללוח
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:
העתקת הקוד ללוח
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:
העתקת הקוד ללוח
With vanilla-intlayer, you can use translations by subscribing to content changes. Here's an example:
העתקת הקוד ללוח
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
);
