多语言网站的Next.js url结构
我正在将我的应用程序迁移到Next.js。目前我的url结构如下:多语言网站的Next.js url结构,next.js,Next.js,我正在将我的应用程序迁移到Next.js。目前我的url结构如下: www.example.com/ <-- English www.example.com/de <-- German www.example.com/page <-- English www.example.com/de/page <-- German module.exports = { i18n: { locales: ['en', 'de'], defaultLocale: '
www.example.com/ <-- English
www.example.com/de <-- German
www.example.com/page <-- English
www.example.com/de/page <-- German
module.exports = {
i18n: {
locales: ['en', 'de'],
defaultLocale: 'en',
},
}
www.example.com/文件夹结构正确,但不需要在[language]
文件夹中编写重复的代码。只需从“主”文件夹导入页面。使用Next.js设置多语言当前有点复杂。以下是目前的情况
如果你想使用图书馆
- 对于无服务器部署,您应该关注这一点。
- 如果没有无服务器部署,您应该关注这一点。
如果你想自己做,你应该修改它们以满足你的需要
- Next.js 9.3版或更高版本
- 9.3以下的Next.js版本
从Next.js 9.4开始,有一个实验性功能“自定义路由”。这提供了一个更简单的解决方案。请参见此处的讨论:
用法示例:
// next.config.js
module.exports = {
async rewrites() {
return [
// Basic `path-to-regexp` usage
// Query object shape: { id: string }
{ source: "/user/:id", destination: "/user_profile" },
// Optional Language
// Query object shape: { lang?: string }
{ source: "/:lang(en|es)?/about", destination: "/about" },
// Advanced rewrite
// Query object shape: { id: string } (in addition to dynamic route param)
{ source: "/u/:id", destination: "/user/:id" }
];
}
};
使用NextJS 10,您可以做到这一点。
您的next.config.js如下所示:
www.example.com/ <-- English
www.example.com/de <-- German
www.example.com/page <-- English
www.example.com/de/page <-- German
module.exports = {
i18n: {
locales: ['en', 'de'],
defaultLocale: 'en',
},
}
defaultLocale
是的语言
然后您可以使用类似库的工具来存储翻译。谢谢。我现在这样做:importindexpage,{getStaticProps,getstaticpath}来自“@/pages/index”;导出{getStaticProps,GetStaticPath};导出默认索引扩展代码>您找到解决方案了吗?请帮帮忙?