このページとあなたの好きなAIアシスタントを使ってドキュメントを要約します
このページのコンテンツはAIを使用して翻訳されました。
英語の元のコンテンツの最新バージョンを見るこのドキュメントを改善するアイデアがある場合は、GitHubでプルリクエストを送信することで自由に貢献してください。
ドキュメントへのGitHubリンクドキュメントのMarkdownをクリップボードにコピー
翻訳
翻訳の定義
intlayer の t 関数を使うと、複数言語でコンテンツを宣言できます。この関数は型安全性を保証し、翻訳が欠けている場合にエラーを発生させます。これは特に TypeScript 環境で役立ちます。
以下は翻訳付きのコンテンツを宣言する例です。
コードをクリップボードにコピー
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>;
ロケールの設定
適切な翻訳処理を行うために、intlayer.config.tsで受け入れるロケールを設定できます。この設定により、アプリケーションがサポートする言語を定義できます。
コードをクリップボードにコピー
import { Locales, type IntlayerConfig } from "intlayer";
const config: IntlayerConfig = {
internationalization: {
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
},
};
export default config;
ロケールの設定
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
);
