Creation:2026-01-21Last update:2026-01-21
将此文档参考到您的 AI 助手ChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
版本历史
- "Init doc"v8.0.02026/1/21
此页面的内容已使用 AI 翻译。
查看英文原文的最新版本Edit this doc
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
Copy doc Markdown to clipboard
intlayer Express 中间件文档
适用于 Express 的 intlayer 中间件会检测用户的区域设置,并通过 res.locals 对象提供翻译函数。它还使您可以在请求处理器的任何地方使用 t 和 getIntlayer 函数。
用法
ts
复制代码
复制代码到剪贴板
import express from "express";import { intlayer } from "express-intlayer";const app = express();app.use(intlayer());app.get("/", (req, res) => { const content = res.locals.t({ zh: "你好", en: "Hello", fr: "Bonjour", }); res.send(content);});描述
该中间件执行以下任务:
- 语言环境检测:它会检查 cookies、HTTP 头(例如
Accept-Language)和 URL 参数以确定用户的语言环境。 - 上下文设置:它会在
res.locals中填充:locale:检测到的语言环境。t:绑定到检测到语言环境的翻译函数。getIntlayer:用于检索绑定到检测到语言环境的词典的函数。
- 异步本地存储(Async Local Storage):它建立了一个上下文,允许在请求流程中使用从
express-intlayer导入的全局t和getIntlayer函数。