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.08/24/2026
If 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 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, containing:locale: The locale to use for this request,locale_storagetaking precedence overlocale_detected.locale_storage: The locale explicitly requested by the client through a cookie or a header.locale_detected: The locale negotiated from the request headers.defaultLocale: The locale configured as fallback inintlayer.config.ts.t: A translation function.getIntlayer: A function to retrieve dictionaries by key.getDictionary: A function to process dictionary objects.
- 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.
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.
Configuration
The plugin reads your intlayer.config.ts file. You can customize 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],
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.
