このページとあなたの好きなAIアシスタントを使ってドキュメントを要約します
このページのコンテンツはAIを使用して翻訳されました。
英語の元のコンテンツの最新バージョンを見るこのドキュメントを改善するアイデアがある場合は、GitHubでプルリクエストを送信することで自由に貢献してください。
ドキュメントへのGitHubリンクドキュメントのMarkdownをクリップボードにコピー
条件付きコンテンツ / Intlayerの条件
条件の仕組み
Intlayerでは、条件付きコンテンツはcond関数を使用して実現されます。この関数は特定の条件(通常はブール値)を対応するコンテンツにマッピングします。このアプローチにより、指定された条件に基づいて動的にコンテンツを選択することが可能になります。React IntlayerやNext Intlayerと統合することで、実行時に提供された条件に応じて適切なコンテンツが自動的に選択されます。
条件付きコンテンツの設定
Intlayerプロジェクトで条件付きコンテンツを設定するには、条件定義を含むコンテンツモジュールを作成します。以下に、さまざまな形式での例を示します。
コードをクリップボードにコピー
import { cond, type Dictionary } from "intlayer";
const myConditionalContent = {
key: "my_key",
content: {
myCondition: cond({
true: "条件がtrueの場合のコンテンツ",
false: "条件がfalseの場合のコンテンツ",
fallback: "条件が失敗した場合のコンテンツ", // オプション
}),
},
} satisfies Dictionary;
export default myConditionalContent;
フォールバックが宣言されていない場合、条件が検証されない場合は最後に宣言されたキーがフォールバックとして使用されます。
React Intlayerでの条件付きコンテンツの使用
To utilize conditional content within a React component, import and use the useIntlayer hook from the react-intlayer package. This hook fetches the content for the specified key and allows you to pass in a condition to select the appropriate output.
コードをクリップボードにコピー
import type { FC } from "react";
import { useIntlayer } from "react-intlayer";
const ConditionalComponent: FC = () => {
const { myCondition } = useIntlayer("my_key");
return (
<div>
<p>
{
/* Output: my content when it's true */
myCondition(true)
}
</p>
<p>
{
/* Output: my content when it's false */
myCondition(false)
}
</p>
<p>
{
/* Output: my content when the condition fails */
myCondition("")
}
</p>
<p>
{
/* Output: my content when the condition fails */
myCondition(undefined)
}
</p>
</div>
);
};
export default ConditionalComponent;
To utilize conditional content in Next.js Client Components, retrieve it via the useIntlayer hook. Here's an example:
コードをクリップボードにコピー
"use client";
import type { FC } from "react";
import { useIntlayer } from "next-intlayer";
const ConditionalComponent: FC = () => {
const { myCondition } = useIntlayer("my_key");
return (
<div>
<p>{myCondition(true)}</p>
<p>{myCondition(false)}</p>
</div>
);
};
export default ConditionalComponent;
To utilize conditional content in Vue components, retrieve it via the useIntlayer hook. Here's an example:
コードをクリップボードにコピー
To utilize conditional content in Svelte components, retrieve it via the useIntlayer hook. The store is accessed with $. Here's an example:
コードをクリップボードにコピー
To utilize conditional content in Preact components, retrieve it via the useIntlayer hook. Here's an example:
コードをクリップボードにコピー
import type { FC } from "preact";
import { useIntlayer } from "preact-intlayer";
const ConditionalComponent: FC = () => {
const { myCondition } = useIntlayer("my_key");
return (
<div>
<p>{myCondition(true)}</p>
<p>{myCondition(false)}</p>
</div>
);
};
export default ConditionalComponent;
To utilize conditional content in SolidJS components, retrieve it via the useIntlayer hook. Here's an example:
コードをクリップボードにコピー
import type { Component } from "solid-js";
import { useIntlayer } from "solid-intlayer";
const ConditionalComponent: Component = () => {
const { myCondition } = useIntlayer("my_key");
return (
<div>
<p>{myCondition(true)}</p>
<p>{myCondition(false)}</p>
</div>
);
};
export default ConditionalComponent;
To utilize conditional content in Angular components, retrieve it via the useIntlayer hook. Here's an example:
コードをクリップボードにコピー
To utilize conditional content with vanilla-intlayer, retrieve it via the useIntlayer hook. Here's an example:
コードをクリップボードにコピー
import { installIntlayer, useIntlayer } from "vanilla-intlayer";
installIntlayer();
const content = useIntlayer("my_key").onChange((newContent) => {
document.getElementById("true-content")!.textContent =
newContent.myCondition(true);
document.getElementById("false-content")!.textContent =
newContent.myCondition(false);
});
// Initial render
document.getElementById("true-content")!.textContent =
content.myCondition(true);
document.getElementById("false-content")!.textContent =
content.myCondition(false);
追加リソース
設定と使用に関する詳細情報については、以下のリソースを参照してください:
これらのリソースは、さまざまな環境やフレームワークでのIntlayerの設定と使用に関するさらなる洞察を提供します。
