You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

next-intl问题:部署至Vercel的共享组件中useTranslation报错

next-intl服务端组件静态渲染问题解答

不需要在每个共享组件中添加unstable_setRequestLocale API。

这个API仅需在所有布局(Layout)和页面(Page)组件中调用即可——这类组件属于路由层级的入口,请求的locale信息会在入口处被设置,之后整个组件树(包括你引入的共享服务端组件)都会继承该locale上下文,共享组件内的useTranslations就能正常获取locale并支持静态渲染。

你只需确保在每个路由的布局和页面文件顶部,先调用unstable_setRequestLocale并传入当前请求的locale值,示例代码如下:

import { unstable_setRequestLocale } from 'next-intl/server';

export default async function Page({ params: { locale } }) {
  unstable_setRequestLocale(locale);
  // 后续渲染逻辑,包括引入你的SomeComponent
}

这样处理后,你的共享服务端组件SomeComponent中的useTranslations就能正常工作,不会触发动态渲染报错,也无需在组件内部重复设置locale。

内容的提问来源于stack exchange,提问作者Keisuke Nagakawa 永川 圭介

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 02:52:08