简体   繁体   English

NextJS i18n - 区域设置未定义

[英]NextJS i18n - Locale getting undefined

I'm trying to migrate my website to NextJS, and I'm having trouble to do some internationalization.我正在尝试将我的网站迁移到 NextJS,但我在进行一些国际化时遇到了麻烦。

I'm following the tutorial from Documentation itself, but my locale in the inspector is coming up as undefined .我正在按照文档本身的教程进行操作,但是我在检查器中的语言环境显示为undefined

What i'm doing wrong?我做错了什么? I'm using the latest version of nextJS.我正在使用最新版本的 nextJS。

Im trying to get some info from console.log.我试图从 console.log 获取一些信息。

console.log("Locale: " + locale);
console.log(router);

and it prints:它打印: 在此处输入图像描述

next.config.js下一步.config.js

module.exports = {
    i18n: {
        locales: ['en-US', 'pt-BR'],
        defaultLocale: 'pt-BR',
    },
}

/pages/index.js /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>
    )
}

/locale/pt.js /locale/pt.js

export default {
    title: "Teste Portugues."
}

/locale/en.js /locale/en.js

export default {
    title: "Test English"
}

Some random info: NextJS Version: 12.0.4 Chrome Version: 96.0.4664.55 Node Version: 17.0.1一些随机信息:NextJS 版本:12.0.4 Chrome 版本:96.0.4664.55 节点版本:17.0.1

This seems to be a bug in older versions of Next.js.这似乎是旧版本 Next.js 中的错误。 yesterday, I had a similar issue, and after upgrading Next, React, and React-dom to their latest version, it worked like charm..昨天,我遇到了类似的问题,在将 Next、React 和 React-dom 升级到最新版本后,它就像魅力一样。

UPDATE: I did a restart of my computer, and after this, 3 days later, when i use console.log({locale}), it get my pt-BR locale normally.更新:我重新启动了我的计算机,在此之后,3 天后,当我使用 console.log({locale}) 时,它会正常获取我的 pt-BR 语言环境。

(And i did nothing more) (我什么也没做)

So, i'll close the thread.所以,我会关闭线程。 Thanks anyway!不管怎么说,还是要谢谢你!

Just restart the server.只需重新启动服务器。 If the next.config.js file is modified, you have to restart your server for the changes to take effect如果修改了 next.config.js 文件,您必须重新启动服务器才能使更改生效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM