Спросите свой вопрос и получите сводку документа, используя эту страницу и выбранного вами поставщика AI
Содержимое этой страницы было переведено с помощью ИИ.
Смотреть последнюю версию оригинального контента на английскомЕсли у вас есть идея по улучшению этой документации, не стесняйтесь внести свой вклад, подав запрос на вытягивание на GitHub.
Ссылка на документацию GitHubКопировать Markdown документа в буфер обмена
Условный Контент / Условие в Intlayer
Как Работает Условие
В Intlayer условный контент достигается с помощью функции cond, которая сопоставляет определенные условия (обычно булевы значения) с соответствующим контентом. Этот подход позволяет динамически выбирать контент на основе заданного условия. При интеграции с React Intlayer или Next Intlayer соответствующий контент автоматически выбирается в зависимости от условия, предоставленного во время выполнения.
Настройка Условного Контента
To utilize conditional content in Svelte components, retrieve it via the useIntlayer hook. The store is accessed with $. 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;
Копировать код в буфер обмена
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 with vanilla-intlayer, retrieve it via the useIntlayer hook. Here's an example:
Использование Условного Контента с 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 в различных средах и фреймворках.
