当前位置: 首页 > wzjs >正文

北京长空建设有限公司网站网络技术网站

北京长空建设有限公司网站,网络技术网站,黄山手机网站建设公司排名,wordpress 团队 主题next-intl 是为 Next.js 提供国际化(i18n)支持的强大库,getTranslations 是它在 服务器端加载翻译文本 时的重要函数,适用于 App Router 场景(如 app/ 目录下的页面、layout、server component)。 ✅ 安装 …

next-intl 是为 Next.js 提供国际化(i18n)支持的强大库,getTranslations 是它在 服务器端加载翻译文本 时的重要函数,适用于 App Router 场景(如 app/ 目录下的页面、layout、server component)。


✅ 安装 next-intl

如果你还没安装:

npm install next-intl

🌍 项目结构建议

.
├── app
│   ├── [locale]             # 动态路由处理多语言
│   │   ├── page.tsx
│   │   ├── layout.tsx
├── messages
│   ├── en.json
│   ├── zh.json
├── middleware.ts

📦 创建多语言 JSON 文件(如 messages/en.json

jsonCopyEdit
{"home": {"title": "Welcome to the site!","description": "This is a demo page."}
}

🛠 配置 middleware.ts

tsCopyEdit
import createMiddleware from 'next-intl/middleware';export default createMiddleware({locales: ['en', 'zh'],defaultLocale: 'en'
});export const config = {matcher: ['/', '/(en|zh)/:path*']
};

📚 使用 getTranslations(核心部分)

👇 示例:app/[locale]/page.tsx

tsxCopyEdit
import { getTranslations } from 'next-intl/server';export default async function HomePage() {const t = await getTranslations('home'); // 读取 "home" 命名空间return (<div><h1>{t('title')}</h1><p>{t('description')}</p></div>);
}

getTranslations服务器端函数,适用于 Server Component 中(也就是不能用于 use client 的组件中)。


📌 getTranslations 的参数说明

tsCopyEdit
const t = await getTranslations(namespace?: string);
  • namespace(可选):对应 JSON 中的顶层键,如 home
  • 返回的 t 是一个类似函数的对象,可用于 t('key') 获取翻译值。

如果你的翻译结构是这样的:

jsonCopyEdit
{"hello": "你好","user": {"greeting": "你好,{name}!"}
}

你可以这样用:

tsCopyEdit
const t = await getTranslations();
t('hello'); // "你好"
t('user.greeting', { name: '小明' }); // "你好,小明!"

⚠ 注意事项

  • getTranslations 只能在 服务器组件、layout.tsx 或 page.tsx 中使用。
  • 对于客户端组件,使用 useTranslations()(配合 use client)代替。
  • 多语言的 JSON 文件应放在统一的 messages/ 目录,并按语言命名。

🎯 小技巧:动态传参

tsCopyEdit
const t = await getTranslations();
const name = 'David';
t('user.greeting', { name }); // "Hello, David!"

💡 配合 layout 使用

tsxCopyEdit
// app/[locale]/layout.tsx
import { getTranslations } from 'next-intl/server';export default async function LocaleLayout({ children }: { children: React.ReactNode }) {const t = await getTranslations('layout');return (<><header>{t('title')}</header>{children}</>);
}

🧪 适用于的场景

  • SEO 友好的 SSR 多语言页面
  • 服务端渲染组件中需要使用国际化
  • App Router 模式下使用 layout.tsxpage.tsxserver component

文章转载自:

http://aaMbBQBu.Lpmjr.cn
http://h1xhX5VX.Lpmjr.cn
http://oH8hncAo.Lpmjr.cn
http://nAZ60LGe.Lpmjr.cn
http://qV6Cc9BF.Lpmjr.cn
http://UNQ4GWSd.Lpmjr.cn
http://HIUdejUn.Lpmjr.cn
http://SNuD24eT.Lpmjr.cn
http://wnSDADZv.Lpmjr.cn
http://KtfHKTfN.Lpmjr.cn
http://kJ3hXOsO.Lpmjr.cn
http://Kpywsmwd.Lpmjr.cn
http://eKLIsv85.Lpmjr.cn
http://8913gVC8.Lpmjr.cn
http://xkvyg3sY.Lpmjr.cn
http://0eAngTBI.Lpmjr.cn
http://q2XjeaZE.Lpmjr.cn
http://HibUr64d.Lpmjr.cn
http://vbvbPtWZ.Lpmjr.cn
http://iAKxPwlt.Lpmjr.cn
http://aN2J0NfY.Lpmjr.cn
http://EKuHPPu9.Lpmjr.cn
http://9en3fYqU.Lpmjr.cn
http://gzxjWymK.Lpmjr.cn
http://VRvFNdpq.Lpmjr.cn
http://2Vy3rGmL.Lpmjr.cn
http://G41ODD4D.Lpmjr.cn
http://YR6ZVv9O.Lpmjr.cn
http://zRWtpO7y.Lpmjr.cn
http://5fOx4KAU.Lpmjr.cn
http://www.dtcms.com/wzjs/735406.html

相关文章:

  • 可信网站验证wordpress google
  • 做问卷用哪个网站企业网站备案域名信息
  • 湖南做网站最厉害的公司wordpress 做门户
  • 网站开发语音占比工程建设教育网站
  • 国家建设执业注册中心网站设计制作活动
  • 罗湖区做网站的公司全网搜索指数
  • wordpress优化网站西宁网站设计企业
  • 虚拟主机网站建设步骤上海环球金融中心门票
  • 网站建设公司咋样网站建设及运维合同
  • 品牌推广策略宿迁seo
  • 做网站设计是什么专业wordpress添加new
  • 谷歌建站多少钱设置什么引擎营销是用户主导的网络营销方式
  • 外贸营销型网站大连网站哪家做的好?
  • 做服务器的网站都有哪些100 款软件app免费下载大全
  • 诸暨哪些公司可以制作网站做网站建设公司赚钱吗
  • html5网站开发技术天津市建设网站
  • 建设公共资源交易中心网站烟台房地产网站建设
  • vps网站管理助手下载网站开发三端指哪三端
  • 济宁市做网站的公司北京百度关键词优化
  • 辉县市工程建设网站建设可信赖的手机网站设计
  • 重庆 网站开发全国城建中心官网入口
  • 徐州网站建设技术外包网站优化的代码
  • 珠海新盈科技网站建设湖北网站建设
  • seo排名优化方式方法seo英文怎么读
  • 福州网站微信公众号西宁网站系统建设
  • 镇江网站建设dmooo做企业网站合同
  • 公司要做好网站怎样做asp网站源码免费版
  • 云主机搭建多个网站网站规划与设计方向
  • 做企业网站需要招标公告上面的技术参数写什么wordpress 邮箱登录
  • 制作网站的视频教程建站一条龙的服务流程是怎么样的