作者:
    Creation:2026-09-19Last update:2026-09-19

    Intlayer 请求上下文键

    Intlayer 导出项用作 Remix 3 中的请求上下文存储标识符。它允许在路由处理程序或自定义中间件内直接从 Remix 上下文对象中检索 Intlayer 状态。

    使用方法

    当 intlayer() 中间件运行时,它会将一个 IntlayerState 对象以 Intlayer 为键存储在请求上下文中。您可以在任何路由处理程序中获取它:

    src/server.ts
    import { createRouter } from "remix/router";
    import { intlayer, Intlayer } from "remix-intlayer";
    
    const router = createRouter({
      middleware: [intlayer()],
    });
    
    router.get("/api/profile", (context) => {
      // 通过 context.get(Intlayer) 访问
      const { locale, defaultLocale, availableLocales } = context.get(Intlayer);
    
      return Response.json({
        locale,
        defaultLocale,
        availableLocales,
      });
    });
    

    您也可以使用直接属性简写 context.intlayer 来访问它:

    ts
    router.get("/api/status", (context) => {
      const currentLocale = context.intlayer.locale;
      return Response.json({ status: "ok", locale: currentLocale });
    });
    

    IntlayerState 结构

    IntlayerState 对象包含:

    属性类型描述
    localeDeclaredLocales为当前请求解析出的语言环境。
    defaultLocaleDeclaredLocales在 intlayer.config.ts 中定义的回退语言环境。
    availableLocalesDeclaredLocales[]项目中配置的所有受支持语言环境列表。

    相关文档