Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
Version History
- "Init doc"v9.4.024/08/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
intlayer Elysia Plugin Documentation
The intlayer plugin for Elysia detects the user's locale and injects an intlayer object into the route context. It also enables the use of global translation functions within the request context.
Usage
Copy the code to the clipboard
The plugin registers its context through a globalderive, which Elysia types asPartial<{ intlayer: IntlayerContext }>. The value is always present at runtime for routes registered after.use(intlayer()), so use the non-null assertion (intlayer!.t) — or optional chaining — to satisfy TypeScript instrictmode.
The same helpers are available as standalone exports, so you can call them without destructuring the route context:
Copy the code to the clipboard
Description
The plugin performs the following tasks:
- Locale Detection: It reads the locale explicitly set by the client from storage (cookie, header), then falls back to the locale negotiated from the
Accept-Languageheader. - Context Injection: It adds an
intlayerproperty to the Elysia route context (see the Route Context table below). - Context Management: It uses
AsyncLocalStorageto manage an asynchronous context, allowing the global Intlayer functions (t,getIntlayer,getDictionary) to access the request-specific locale without passing the context object around. - Dictionary Preparation: It calls
prepareIntlayerwhen the plugin is created, so the dictionaries are built when the app boots.
Route Context
Open the table in a modal to view all data content clearly
| Property | Type | Description |
|---|---|---|
locale | Locale | The locale to use for this request, locale_storage taking precedence over locale_detected. |
locale_storage | Locale (optional) | The locale explicitly requested by the client through a cookie or a header. |
locale_detected | Locale | The locale negotiated from the request headers. |
defaultLocale | Locale | The locale configured as fallback in intlayer.config.ts. |
t | TranslateFunction | A translation function. |
getIntlayer | typeof getIntlayer | A function to retrieve dictionaries by key. |
getDictionary | typeof getDictionary | A function to process dictionary objects. |
Unlike the Node-based Intlayer plugins,elysia-intlayerrelies onAsyncLocalStorageinstead ofcls-hooked, becausecls-hookeddepends onasync_hooks.createHook, which Bun does not implement.
The request context is released once the response is mapped, so the standalone helpers never resolve against an already terminated request. When called outside of a request handled by the plugin, they fall back to the configured default locale.
Locale Resolution Order
By default, the plugin resolves the locale in this order:
- The
INTLAYER_LOCALEcookie. - The
x-intlayer-localeheader. - The
Accept-Languageheader negotiation. - The configured
defaultLocale.
Copy the code to the clipboard
Configuration
The plugin reads your intlayer.config.ts file. You can customise the cookie and header used for locale detection:
Copy the code to the clipboard
import { Locales, type IntlayerConfig } from "intlayer";
const config: IntlayerConfig = {
internationalization: {
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
defaultLocale: Locales.ENGLISH,
},
routing: {
storage: [
{ type: "header", name: "my-locale-header" },
{ type: "cookie", name: "my-locale-cookie" },
],
},
};
export default config;
For more information on configuration, visit the configuration documentation.
