Creation:2026-01-21Last update:2026-01-21

    intlayer Fastify Plugin Documentation

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

    Usage

    import Fastify from "fastify";
    import { intlayer } from "fastify-intlayer";
    
    const fastify = Fastify();
    
    fastify.register(intlayer);
    
    fastify.get("/", async (req, reply) => {
      const content = req.intlayer.t({
        en: "Hello",
        fr: "Bonjour",
      });
    
      return content;
    });

    Description

    The plugin performs the following tasks:

    1. Locale Detection: It analyzes the request (headers, cookies, etc.) to determine the user's preferred locale.
    2. Request Decoration: It adds an intlayer property to the FastifyRequest object, containing:
      • locale: The detected locale.
      • t: A translation function.
      • getIntlayer: A function to retrieve dictionaries.
    3. Context Management: It uses cls-hooked to manage an asynchronous context, allowing global Intlayer functions to access the request-specific locale.