Skip to content

Recommended approach for handling country prefix along with locale #1354

Answered by amannn
im-luka asked this question in Q&A
Discussion options

You must be logged in to vote

Glad to hear if next-intl is working well for you! 🙌

Not sure if I'm missing something, but can you generate locales and prefixes dynamically? Maybe something like:

import {defineRouting} from "next-intl/routing";

const countries = ['DE', 'AT', /* ... */];
const languages = ['de', 'en', /* ... */];

export const routing = defineRouting({
  locales: countries.map((country) => languages.map((language) => `${language}-${country}`)).flat(),
  defaultLocale: "en-DE",
  localePrefix: {
    mode: "always",
    prefixes: Object.fromEntries(
      countries.map((country) => languages.map((language) => [`${language}-${country}`, `/${country}/${language}`])).flat()
    )
  }
});

If type safety for …

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@im-luka
Comment options

@amannn
Comment options

@im-luka
Comment options

@amannn
Comment options

@im-luka
Comment options

Answer selected by im-luka
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants