May*_*ser 5 javascript next.js
我正在尝试将我的网站迁移到 NextJS,但在进行一些国际化时遇到了麻烦。
我正在遵循文档本身的教程,但检查器中的区域设置显示为undefined。
我做错了什么?我正在使用最新版本的 nextJS。
我试图从 console.log 获取一些信息。
console.log("Locale: " + locale);
console.log(router);
Run Code Online (Sandbox Code Playgroud)
next.config.js
module.exports = {
i18n: {
locales: ['en-US', 'pt-BR'],
defaultLocale: 'pt-BR',
},
}
Run Code Online (Sandbox Code Playgroud)
/pages/index.js
import Head from 'next/head'
import { useRouter } from 'next/router'
import pt from '../locale/index/pt'
import en from '../locale/index/en'
export default function Home() {
const router = useRouter();
const { locale } = router;
const t = locale === 'pt' ? pt : en;
return (
<div className="container">
<Head>
<title>{t.title}</title>
</Head>
</div>
)
}
Run Code Online (Sandbox Code Playgroud)
/locale/pt.js
export default {
title: "Teste Portugues."
}
Run Code Online (Sandbox Code Playgroud)
/locale/en.js
export default {
title: "Test English"
}
Run Code Online (Sandbox Code Playgroud)
一些随机信息: NextJS 版本:12.0.4 Chrome 版本:96.0.4664.55 Node 版本:17.0.1
更新:我重新启动了计算机,三天后,当我使用 console.log({locale}) 时,它正常获取我的 pt-BR 区域设置。
(我什么也没做)
那么,我将关闭线程。不管怎么说,还是要谢谢你!
| 归档时间: |
|
| 查看次数: |
4506 次 |
| 最近记录: |