Creation:2026-01-29Last update:2026-01-29

    intlayer Hono Middleware Documentation

    The intlayer middleware for Hono detects the user's locale and populates the context object with Intlayer functions. It also enables the use of global translation functions within the request context.

    Usage

    import { Hono } from "hono";
    import { intlayer } from "hono-intlayer";
    
    const app = new Hono();
    
    app.use("*", intlayer());
    
    app.get("/", async (c) => {
      const t = c.get("t");
      const content = t({
        en: "Hello",
        fr: "Bonjour",
      });
    
      return c.text(content);
    });

    Description

    The middleware performs the following tasks:

    1. Locale Detection: It analyzes the request (headers, cookies, etc.) to determine the user's preferred locale.
    2. Context Population: It adds Intlayer data to the Hono context, accessible via c.get(). This includes:
      • locale: The detected locale.
      • t: A translation function.
      • getIntlayer: A function to retrieve dictionaries.
      • getDictionary: A function to process dictionary objects.
    3. Context Management: It uses cls-hooked to manage an asynchronous context, allowing global Intlayer functions (t, getIntlayer, getDictionary) to access the request-specific locale without passing the context object.