question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Locale Sub-routing doesn't work

See original GitHub issue

Hi,

I’ve got this configuration on my next js with next-translate:

Inside next.config.js

const nextTranslate = require('next-translate');

module.exports = nextTranslate({
   webpack: (config, { isServer, webpack }) => {
      return config;
   },
   target: 'serverless'
});

And inside i18n.json

{
   "locales": [
      "en",
      "gr"
   ],
   "defaultLocale": "gr",
   "pages": {
      "*": [
         "common"
      ],
      "/": [
         "home"
      ],
      "/about": [
         "about"
      ]
   }
}

The problem I’m facing is when switching the locale to en all blog posts are broken e.g http://localhost:3000/blog/test <- Works

http://localhost:3000/en/blog/test <- 404 not found

Any ideas what I should do to make the sub-routing work correctly?

Thanks

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
aralrocacommented, Sep 15, 2021

@ykyp You need to add the locales to the getStaticPaths:

export async function getStaticPaths({ locales }) {
   const paths = getAllPostIds(locales);
   return {
      paths,
      fallback: false,
   };
}

and your function:

export function getAllPostIds(locales) {
   const fileNames = fs.readdirSync(postsDirectory);

   return fileNames.flatMap((fileName) => {
      const params = {
         id: fileName.replace(/\.md$/, ""),
      }

      return [
         { params },
         ...locales.map(locale => ({ params, locale })),
      ]
   });
}

This is not handled by next-translate and is a Next.js requirement.

1reaction
ykypcommented, Sep 19, 2021

Thanks a lot @aralroca confirming this has now fixed my issue 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

Locale Sub-routing doesn't work · Issue #627 - GitHub
Hi, I've got this configuration on my next js with next-translate: Inside next.config.js const nextTranslate = require('next-translate'); ...
Read more >
Next.js router locale issue - Stack Overflow
When routing using localized routes, you need to specify the locale by including additional options in the router.push call.
Read more >
Advanced Features: Internationalized Routing - Next.js
Next.js has built-in support for internationalized routing and language ... There are two locale handling strategies: Sub-path Routing and Domain Routing.
Read more >
RouterOutlet - Angular
A router outlet emits an activate event when a new component is instantiated, deactivate event when a component is destroyed. An attached event...
Read more >
Routing (Symfony Docs)
Sub -Domain Routing; Localized Routes (i18n); Stateless Routes ... The routing configuration defines which action to run for each incoming URL.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found