【问题标题】:NextJS i18next Nextjs Localize RoutesNextJS i18next Nextjs 本地化路由
【发布时间】:2019-12-05 14:41:32
【问题描述】:

我已经为我的 NextJS 网站完全实现了 next-i18next。一切正常:

  • 使用 i18next 中间件自动检测语言
  • 使用语言选择按钮更改语言
  • 仅在相关命名空间等中加载的翻译

但是,我想本地化路线名称,以便它们反映设置的语言。

我了解如何使用 next-routes 完成此操作,并且此交换特别有用:

How do I localize routes with next.js and next-i18next?

在该示例中,路由设置如下:

routes.add('en-home', '/', 'index')
routes.add('de-home', '/de', 'index')
routes.add('en-test', '/test', 'test')
routes.add('de-test', '/de/prufung', 'test')

我尝试使用以下模式作为示例从 next-routes 实现 Route 和 Link:

  1. 从i18n.language获取当前语言
  2. 根据语言设置链接url
<Link route={`${lang}-home`}></Link>

再一次,对下一个路由路由器使用类似的模式,例如:

Router.pushRoute(`${lang}-home`)

但是,这不起作用。

我在引用的线程中注意到@samuelg0rd0n 评论说他创建了一个自定义链接组件。

请任何人指导我如何根据网站设置的语言更改所有路线?

谢谢!

【问题讨论】:

  • 嗨,保罗,欢迎来到 StackOverflow!我刚刚为您发送了一个专门的示例。我希望它可以帮助。
  • @FrancisRodrigues 你能把同样的例子发给我吗?我面临同样的问题,需要实现自定义链接,可以使用 routes.js 和 i18ln.js
  • 你找到解决办法了吗?你能分享一下吗?谢谢

标签: next.js i18next


【解决方案1】:

我强烈建议您尝试官方的 Next.js 示例:

Next.js -- 命名路由示例(next-routes)

https://github.com/zeit/next.js/tree/canary/examples/with-next-routes

但我发现它需要额外的服务器,如果我们将应用程序结构自定义为我自己的项目示例,它也会失败:

| src/app
   | -- pages
       | -- post.js
       | -- category.js
   | -- server.js (for routes)

| src/functions
   | -- index.js (lambda function for server-side)

如果我运行next src/app,它将失败并出现以下错误:

Error: > Couldn't find a `pages` directory. Please create one under the project root
    at findPagesDir (/home/paneladm/projects/paneladm-dev/travel-website/node_modules/next/dist/lib/find-pages-dir.js:3:170)
    at new DevServer (/home/paneladm/projects/paneladm-dev/travel-website/node_modules/next/dist/server/next-dev-server.js:1:3491)
    at createServer (/home/paneladm/projects/paneladm-dev/travel-website/node_modules/next/dist/server/next.js:2:105)
    at Object.<anonymous> (/home/paneladm/projects/paneladm-dev/travel-website/src/app/server.js:14:13)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)

Next.js 团队现在正在研究一个简单的解决方案

这添加了一个示例,展示了如何通过利用 Next.js 内置的新自定义路由支持来消除使用 next-routes 的现有应用程序对自定义服务器的需求

您应该将next 更新为9.1.+ 并实现您自己的路由,如下所示:

const nextRoutes = require('next-routes');
const routes = (module.exports = nextRoutes());

routes.add('category', '/category/:slug');
routes.add('post', '/post/:lang/:slug');

在您的next.config.js 文件中,启用experimental 属性并实现rewrites 方法如下:

const { routes } = require('./routes');

...

experimental: {
    rewrites() {
      const rewrites = [];

      for (const route of routes) {
        if (route.pattern !== route.page) {
          rewrites.push({
            source: route.pattern,
            destination: route.page,
          });
        }
      }

      return rewrites;
    }
  },

现在你的结构应该是:

| src/app
   | -- pages
       | -- post.js
       | -- category.js

但是,如果您想自定义页面,请尝试像这样更改路线:

...

routes.add('category/[slug]', '/category/:slug');
routes.add('post/[slug]', '/post/:lang/:slug');

它将指向pages --&gt; category --&gt; [slug].js 文件。

在新的PR implementation 文件中查看更多详细信息。

参考

【讨论】:

    猜你喜欢
    • 2021-02-25
    • 2021-06-16
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 2020-06-03
    • 2021-08-24
    相关资源
    最近更新 更多