-
Hi, love the library, use it in every app! However, I encountered a problem and I am not sure what is the go-to way to resolve it. As the title suggest, our team is building an application that has both, multi-language and multi-country support. In the app, user has ability to change both country and language. All locales are available to all countries, but every country has it's own products, prices, orders etc. There are two dropdowns, one for country and one for locale. It looks something like this (demo example): We are currently using However, the client came to us with request that they longer don't like this structure and would like to include My question is - what would be recommended approach in this case, if there is any? I went through the docs, discussions and issues and the closest thing I found is #653 but the problem is:
Current, "hacky" solution we have so far is that we add country prefix to all of our
import { defineRouting } from "next-intl/routing";
import { createSharedPathnamesNavigation } from "next-intl/navigation";
export const routing = defineRouting({
locales: [
"AT-de",
"AT-en",
"DE-de",
"DE-en",
"HR-de",
"HR-en",
"IT-de",
"IT-en",
],
defaultLocale: "DE-en",
localePrefix: {
mode: "always",
prefixes: {
"AT-de": "/at/de",
"AT-en": "/at/en",
"DE-de": "/de/de",
"DE-en": "/de/en",
"HR-de": "/hr/de",
"HR-en": "/hr/en",
"IT-de": "/it/de",
"IT-en": "/it/en",
},
},
});
export const { Link, redirect, usePathname, useRouter } =
createSharedPathnamesNavigation(routing);
import createMiddleware from "next-intl/middleware";
import { routing } from "./i18n/routing";
export default createMiddleware(routing);
export const config = {
matcher: ["/", "/(at/de|at/en|de/de|de/en|hr/de|hr/en|it/de|it/en)/:path*"],
}; With that, we could always split Thank you and all the best! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Glad to hear if Not sure if I'm missing something, but can you generate 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 the routing is important to you, you might have to add some TypeScript tricks to this, but for the JavaScript part this might work. After that, in Could that work for you? One important note: A |
Beta Was this translation helpful? Give feedback.
Glad to hear if
next-intl
is working well for you! 🙌Not sure if I'm missing something, but can you generate
locales
andprefixes
dynamically? Maybe something like:If type safety for …