Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
Version History
- "Introduce select content"v9.1.030/07/2026
The content of this page was translated using an AI.
See the last version of the original content in EnglishIf you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy doc Markdown to clipboard
Select Content / Intlayer
How Select Works
In Intlayer, select content is achieved through the select function, which maps arbitrary string values to their corresponding content. It is the equivalent of the ICU {value, select, …} message, or similar to a switch statement in your application's code.
Use select when the discriminant is an arbitrary string: such as a status, a plan, a platform, or a role. For other discriminants, Intlayer provides dedicated nodes:
Open the table in a modal to view all data content clearly
| Discriminant | Node |
|---|---|
| Quantity | enu() |
| Boolean | cond() |
| Gender | gender() |
| Any other string | select() |
Setting Up Select Content
To set up select content in your Intlayer project, create a content module that includes your select definitions. Below are examples in different formats.
Copy the code to the clipboard
import { select, type Dictionary } from "intlayer";
const myPostContent = {
key: "my_key",
content: {
publishStatus: select({
draft: "This post is a draft",
published: "This post is live",
scheduled: "This post is scheduled",
fallback: "Unknown status", // optional
}),
},
} satisfies Dictionary;
export default myPostContent;
If nofallbackis declared, the last declared key is considered as the fallback if the provided value does not match any of the declared cases: exactly the same as forcond()andgender()contracts.
Type Safety
The accepted argument is inferred from the declared cases:
- Without a
fallback, only the declared cases are accepted: a typo will result in a type error. - With a
fallback, any string is accepted (as the fallback covers unmatched values), whilst the declared cases still provide autocomplete.
Why not use a regular object?
It could be tempting to declare a regular object and index it with the runtime value:
Copy the code to the clipboard
The Intlayer compiler parses your source code to eliminate unused content and minify the remaining keys. A dynamically computed access (obj[expr]) cannot be resolved statically, hence the entire branch is marked as opaque: it will be kept in the bundle, and its keys will not be minified.
By using select(), the case resolution happens inside a function call rather than a property access. The compiler sees it as a single, static field access and properly optimises the node exactly as it does for enu(), cond(), or gender():
Copy the code to the clipboard
Using Select Content
To utilise select content in 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 a value to select the appropriate output.
Copy the code to the clipboard
import type { FC } from "react";
import { useIntlayer } from "react-intlayer";
const PostStatus: FC = () => {
const { publishStatus } = useIntlayer("my_key");
return (
<div>
<p>
{
/* Output: This post is a draft */
publishStatus("draft")
}
</p>
<p>
{
/* Output: This post is live */
publishStatus("published")
}
</p>
<p>
{
/* Output: Unknown status */
publishStatus("Archived")
}
</p>
</div>
);
};
export default PostStatus;
To utilise select content in Next.js Client Components, fetch the content via the useIntlayer hook. Here's an example:
Copy the code to the clipboard
"use client";
import type { FC } from "react";
import { useIntlayer } from "next-intlayer";
const PostStatus: FC = () => {
const { publishStatus } = useIntlayer("my_key");
return (
<div>
<p>{publishStatus("draft")}</p>
<p>{publishStatus("published")}</p>
</div>
);
};
export default PostStatus;
To utilise select content in Vue components, fetch the content via the useIntlayer hook. Here's an example:
Copy the code to the clipboard
To utilise select content in Svelte components, fetch the content via the useIntlayer hook. The store is accessed using $. Here's an example:
Copy the code to the clipboard
To utilise select content in Preact components, fetch the content via the useIntlayer hook. Here's an example:
Copy the code to the clipboard
import type { FC } from "preact";
import { useIntlayer } from "preact-intlayer";
const PostStatus: FC = () => {
const { publishStatus } = useIntlayer("my_key");
return (
<div>
<p>{publishStatus("draft")}</p>
<p>{publishStatus("published")}</p>
</div>
);
};
export default PostStatus;
To utilise select content in SolidJS components, fetch the content via the useIntlayer hook. Here's an example:
Copy the code to the clipboard
import type { Component } from "solid-js";
import { useIntlayer } from "solid-intlayer";
const PostStatus: Component = () => {
const { publishStatus } = useIntlayer("my_key");
return (
<div>
<p>{publishStatus("draft")}</p>
<p>{publishStatus("published")}</p>
</div>
);
};
export default PostStatus;
To utilise select content in Angular components, fetch the content via the useIntlayer hook. Here's an example:
Copy the code to the clipboard
To utilise select content with vanilla-intlayer, fetch the content via the useIntlayer function. Here's an example:
Copy the code to the clipboard
import { installIntlayer, useIntlayer } from "vanilla-intlayer";
installIntlayer();
const content = useIntlayer("my_key").onChange((newContent) => {
document.getElementById("status")!.textContent =
newContent.publishStatus("draft");
});
// Initial render
document.getElementById("status")!.textContent = content.publishStatus("draft");
Composing Select with Other Nodes
Because each case is housing a complete content node, select can be combined with t(), insert(), md(), etc.:
Copy the code to the clipboard
Copy the code to the clipboard
Migrating from ICU select
Messages using the ICU select argument are imported as a select node:
Copy the code to the clipboard
Will become:
Copy the code to the clipboard
The ICU other case is renamed to fallback, which is the Intlayer canonical name for all catch-all cases. The second argument holds the ICU variable name so that when exported, the message reverts back to the exact same ICU string.
Please note that ICUselectmessages where the cases are gender values (male/female/other) are imported as agendernode instead.
Additional Resources
For more detailed information on configuration and usage, please refer to the following resources:
These resources provide further insights into setting up and using Intlayer across various environments and frameworks.