05 / 10

How would you implement slug-based routing that also supports localization? (e.g. /en/products/shoes vs /fr/produits/chaussures)

Difficulty: 6/10
dynamic routing, i18n, slug generation

Implement localized slug-based routing using either Next.js built-in i18n routing with a catch-all dynamic route and a slug mapping system, or use a dedicated library like next-roots that generates language-specific route folders at build time

Implementing localized slug-based routing in Next.js requires handling both the language prefix and the translated path segments. For URLs like /en/products/shoes and /fr/produits/chaussures, you need a system that maps the same underlying content to different URL structures based on language. Next.js offers several approaches: using built-in i18n routing with a dynamic [locale] folder and a slug translation layer, or using specialized libraries like next-roots that pre-generate all language-specific routes at build time. The choice depends on whether you need static generation for all language variants or prefer a more dynamic approach [citation:5][citation:2][citation:6].

Next.js has built-in internationalized routing since version 10.0.0, which handles locale prefixes automatically [citation:5]. For slug translation, you can combine this with a dynamic route structure. In your next.config.js, configure your supported locales. Then create a dynamic route folder [locale] that contains your pages. For the slug translation itself, you need to implement a mapping system—either in your data fetching logic or via a middleware—that converts the localized slug to a canonical identifier and fetches the correct content [citation:1][citation:5]. The Optimizely documentation shows this pattern with a [locale]/[slug] structure where the page component fetches content based on both parameters [citation:1].

Built-in i18n Configuration

The next-intl library provides a more complete solution for internationalization, including routing, translations, and locale management. It integrates seamlessly with Next.js App Router and handles the complexity of localized routes, including slug translation. You define your routing configuration, and next-intl provides middleware for locale negotiation and components for navigation that automatically handle translated paths [citation:2]. This approach gives you both the locale prefix routing and the ability to manage translated content through your data layer.

next-intl Setup for Localized Routing

For projects that need fully static, pre-generated routes with translated slugs, next-roots offers an alternative approach. Instead of using dynamic [locale] segments, it generates actual folders for each language at build time. You maintain your original routes in a roots folder, and next-roots creates language-specific routes (e.g., (en)/products/shoes and fr/produits/chaussures) by reading translation files. This approach gives you the full benefits of static generation and type-safe links, but requires running a generation step whenever routes or translations change [citation:3][citation:6].

next-roots Configuration Example

Regardless of which routing approach you choose, you need a strategy for slug translation in your data layer. The Contentful documentation demonstrates fetching content based on both locale and slug parameters, where each content entry stores its slug for each supported language [citation:7]. In your API or database, you should store a mapping of (locale, slug) → contentId or include all language slugs on each content item. This allows your getProductBySlug(slug, locale) function to retrieve the correct content regardless of which language's slug is used in the URL [citation:7].

Important Considerations
  1. 1

    SEO implications: Using different URLs for different languages is good for SEO, but ensure you implement proper hreflang tags to indicate language alternatives to search engines [citation:5].

  2. 2

    Static generation: For dynamic routes with getStaticProps, you must return all locale variants from getStaticPaths to pre-render every language version [citation:5].

  3. 3

    Default locale prefix: By default, Next.js does not prefix the default locale (e.g., /en for English). If you want all languages to have prefixes, you can configure this via middleware or use libraries like next-intl that support this [citation:2][citation:5].

  4. 4

    Dynamic parameters: When using libraries like next-roots, you get type-safe links and automatic locale detection in your navigation components, reducing the risk of broken links [citation:3][citation:6].

Scenario Questions

0-2 years experience

  1. 1We need a page that shows a product based on a slug, and the URL should include the locale like /en/products/shoes. How would you set up the file structure and getStaticPaths in Next.js to handle this?
  2. 2If a user navigates to /fr/produits/chaussures and the slug doesn't exist in the French catalog, what would you render and how would you detect that case?
  3. 3What happens if you forget to include the locale param in getStaticProps? Explain the result.

2-5 years experience

  1. 1During a recent feature, you added localized slugs but noticed that navigating from /en/products/shoes to /fr/produits/chaussures triggers a full page reload. What could be causing that and how would you fix it?
  2. 2You need to support fallback rendering for new products while keeping SEO‑friendly URLs for each locale. How would you configure getStaticPaths and fallback to meet both requirements?
  3. 3A teammate reports that the French slug '/fr/produits/chaussures' resolves to the English product page. Walk me through how you would debug the routing mismatch.

5-8 years experience

  1. 1At scale we have thousands of products and ten locales. Discuss the trade‑offs between generating all localized pages at build time versus using on‑demand ISR. Which approach would you choose and why?
  2. 2Explain how you would design a reusable Next.js utility that maps a product ID to the correct localized slug, handling edge cases like slug collisions across languages.
  3. 3Consider a CDN cache that stores pages by URL. How would you ensure that cache keys correctly differentiate locales and avoid serving the wrong language version after a content update?

8+ years experience

  1. 1Our monorepo contains multiple Next.js apps that share a common i18n routing strategy. How would you architect a shared library or convention to enforce consistent slug‑based localization across teams, while allowing each app to customize its own locale fallback rules?
  2. 2We need to migrate an existing legacy Rails site with locale‑prefixed paths to Next.js without downtime. Outline a migration plan that preserves SEO rankings, handles redirects, and gradually phases out the old routing.
  3. 3Discuss the long‑term maintenance implications of storing localized slugs in a database versus generating them from a translation file. Which would you recommend for a global e‑commerce platform and why?

Follow-up Questions

  • How would you test the localized routing implementation?
  • What edge cases do you think could break this setup?
  • How does this approach impact SEO and sitemap generation?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.