Creation:2026-01-21Last update:2026-01-21
将此文档参考到您的 AI 助手ChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
版本历史
- "初始化文档"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 Fastify 插件文档
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.
用法
ts
复制代码
复制代码到剪贴板
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;});描述
该插件执行以下任务:
- 区域设置检测:它分析请求(headers、cookies 等)以确定用户偏好的区域设置。
- 请求装饰:它向
FastifyRequest对象添加一个intlayer属性,包含:locale:检测到的区域设置。t:翻译函数。getIntlayer:用于检索字典的函数。
- 上下文管理:它使用
cls-hooked管理异步上下文,使全局 Intlayer 函数能够访问请求特定的区域设置。