NextJS 国际化路由(本地化 slugs)

rkr*_*s26 5 localization routes internationalization next.js

我有一个使用 i18n 进行国际化路由的网站,它工作正常,但现在我们也想翻译、本地化 slugs。

页面结构

例如,我们有这个路线/integrations-and-plugins 例如:3 个语言环境,en de 和 hu 我们希望:

  • /en/集成和插件/
  • /de/集成和插件/
  • /hu/integraciok-es-pluginok/

(+此外它还有一个集成和插件/*id,但并不重要)

这是我的下一个配置相关部分:

const bundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: !!process.env.BUNDLE_ANALYZE,
})

module.exports = bundleAnalyzer({
  images: {
    domains: ['cdn.builder.io'],
  },
  async headers() {
    return [
      {
        source: '/:path*',
        headers: [
          // this will allow site to be framed under builder.io for wysiwyg editing
          {
            key: 'Content-Security-Policy',
            value: 'frame-ancestors https://*.builder.io https://builder.io',
          },
        ],
      },
    ]
  },
  async rewrites() {
    return [
      {
        source: '/hu/integracios-es-ellenorzesi/',
        destination: '/hu/integrations-and-plugins/',
        locale: false,
      },
      {
        source: '/de/integracios-es-ellenorzesi/',
        destination: '/de/integrationen-und-plugins/',
        locale: false,
      },
     
    ]
  },
  // async redirects() { //WE DON'T WANT TO USE REDIRECTS BECAUSE OF SEO PURPOSES
  //   return [
  //     {
  //       source: '/hu/integracios-es-ellenorzesi/',
           destination: '/hu/integrations-and-plugins/',
  //       permanent: true,
  //       locale: false,
  //     },
  //     {
  //       source: '/de/integracios-es-ellenorzesi/',
           destination: '/de/integrationen-und-plugins/',
  //       permanent: true,
  //       locale: false,
  //     },
  //   ]
  // },
  i18n: {
    locales: ['default', 'en', 'hu', 'de', 'cz', 'eu', 'sl'],
    defaultLocale: 'default',
    localeDetection: true,
  },
  trailingSlash: true,
})
Run Code Online (Sandbox Code Playgroud)

据我所知,Next.js 目前不支持此功能(https://github.com/vercel/next.js/discussions/18485

通过重写,我只能实现内容是正确的,但 url 将保持不变,虽然重定向可以用于更改 url,但我们不想重定向,因为 SEO,而且它是根本不是最好的选择。

希望有人遇到同样的问题,并可以帮助我找出翻译 url slugs 的最佳方法:

p.d*_*iel -1

NextJS 存储库上有一个与您的问题相关的问题主题,可能会帮助您遵循社区方法:https ://github.com/vercel/next.js/discussions/18485