Feature requestIs your feature request related to a problem? Please describe.Updated answer: Yes. The i18n-feature of next.js hides the
Original, (too) modest answer: Not really. It would just allow the same behaviour we have with our current Describe the solution you'd likeI would like to ask for the feature basically proposed here: #17078 (comment) by @Vadorequest, which is to optionally enable a prefix for the default locale. Example: Describe alternatives you've consideredNote/Update: These are ideas summarized from the comments below
Note to the communityPlease refrain from posting comments like "I want this too", "+1" and so on. Instead use the ":+1:" below the issue to upvote this feature request. |
Replies: 51 comments 134 replies
|
Ideal, this should be configurable with a boolean. |
|
+1 for this request |
|
FYI if there are more upvotes on the above discussion, then it'll increase the probability for it to be implemented. Source: #18582 |
|
This seem to be a fairly common pattern out in the wild, eg.: It would definitely be needed to support our own existing Next.js site which has the locale in every path: I considered allowing the prefixless version for our default locale (en-US) and using |
|
I made a PR with a fix for our Problem: #19035 |
|
hello guys any news on this? Could be good to have this option and avoid the redirect solution. |
|
Hi |
|
We would like to have this too. |
|
@timneutkens, is there any chance this feature and/or #19035 will be checked and implemented anytime soon? Both this post and the PR have quite some upvotes now :) |
|
We found a Workaround for our Project which is a bit hacky but okay for us at the moment. Basically we add a fake language and detect the language by our own. No guarantee that the code below works :) I added the language "fake" in the next.config as third and default language: in the _app.tsx we try to detect the language / default and redirect to them |
|
need this |
|
Just for information: I closed my PR because I think the implementation should be done by someone who has a deeper understanding of the i18n integration in Next.js |
|
I would benefit from having this feature too. |
|
Hi @timneutkens, Can you give an update on this? Can we wait for this feature or are you guys not going to implement it? |
|
I have a possible alternative solution that allows you to continue to use automatic static optimization and without any custom code other than a few config lines. Step 1: Add a
|
|
This approach works well without domains. So, for example, if I have a domain that covers a few languages, like *.ch. Say, I have a site with 10 domains and I want the default-locale prefixed in all of them to treat the languages equally. Do I need then 10 placeholder locales (default-de, default-com, etc.) & a huge _middleware.ts? Is there any better way of solving this? |
|
I do need such a feature |
|
The accepted answer doesn't work when using the Edge Functions in Netlify. I added the following changes to make it work. // pages/_middleware.ts
import { NextRequest, NextResponse } from "next/server";
const PUBLIC_FILE = /\.(.*)$/;
export function middleware(request: NextRequest) {
if (process.env.NODE_ENV === "development") {
const shouldHandleLocale =
!PUBLIC_FILE.test(request.nextUrl.pathname) &&
!request.nextUrl.pathname.includes("/api/") &&
request.nextUrl.locale === "default";
if (shouldHandleLocale) {
const url = request.nextUrl.clone();
url.pathname = `/de${request.nextUrl.pathname}`;
return NextResponse.redirect(url);
}
return undefined;
} else {
const shouldHandleLocale =
!PUBLIC_FILE.test(request.nextUrl.pathname) &&
!request.nextUrl.pathname.includes("/api/") &&
!request.nextUrl.pathname.startsWith("/en/") && // Netlify Edge Functions don't have request.nextUrl.locale so we need to manually check the path
request.nextUrl.pathname !== "/en" &&
!request.nextUrl.pathname.startsWith("/de/") &&
request.nextUrl.pathname !== "/de";
if (shouldHandleLocale) {
const url = request.nextUrl.clone();
url.pathname = `/de${request.nextUrl.pathname}`;
return NextResponse.redirect(url);
}
return undefined;
}
}// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
i18n: {
locales: ["default", "en", "de"],
defaultLocale: "default",
localeDetection: false,
},
trailingSlash: true,
};
module.exports = nextConfig; |
|
I know that everything here has been said already but I think that it should be much easier to setup, without any middleware since it breaks website logic. For example if the user changes locale to default one on purpose (which changes url to /) and refreshes the website, the app again uses browser's locale. What we did was pretty simple (but far from ideal in my opinion), we've added 'default' language definition in i18n next to the 'en', and set it as default one as well. We've left the detectlocale set to true since it's a pretty nice feature ;). In order to receive locale in the pages where we need the information, we've made custom hook that checks if the active locale is set to 'default' and otherwise return 'en'. For me the best option would be to add a flag that allows a user to force locale in the url. Hope that helps someone :) |
|
Since v12.2 A quick solution would be to filter out those internal requests prior to any redirection export async function middleware(req: NextRequest) {
if (req.nextUrl.pathname.startsWith("/_next")) {
return res;
}
// rest of locale logic
} |
|
I can't imagine why it would be so difficult to optionally enable force default locale prefix in the url, it is a much requested feature and the accepted solution with the middleware introduces unnecessary complexity and even drops performance. I can understand that maybe that feature needs a lot of work and re-coding but I would wish someone to state that. I am going to open a new feature request discussion mentioning this in case that this one here is no longer being monitored due to having the "answered flag". ( #38387) |
|
Is there any way to do this the opposite way, so that the defaultLocale can only be loaded on the domain and not as well in a subfolder? domain.com - defaultLocale |
|
So using the suggested middleware in the docs https://nextjs.org/docs/advanced-features/i18n-routing#prefixing-the-default-locale actually breaks any statically generated pages for me but only if they are deployed to Vercel. All paths I return from getStaticPaths first renders correctly, but an instant later I get a client side re-render where none of my props supplied by getStaticProps are defined. The paths that are not returned from getStaticPaths however works as they should. They are generated on request and after that I can still access them without errors (this time fast, as they are now generated). After digging quite a bit it seems it could be related to what is returned by the If I remove the middleware everything works as it should again, except I don't get the wanted redirect for default locale. |
|
If you're interested in handling this in the |
|
@jamimeson @ajmnz @RazaGR @talatkuyuk @santyas @PascalPixel @angeloreale @elias-thok @AustinShelby @igorzinar I'm asking this because I'm not aware of what Nextjs does with the returned NextResponse instance, and in locales middleware from Nextra somehow modifies the |
|
The solution presented here and also on https://nextjs.org/docs/app/building-your-application/routing/internationalization doesn't work if you're incrementally adopting the /app folder. I'm using /pages folder with a following And a check/redirect on So, we don't have a But since under /pages we don't have that but actually the first parameter that we need for our app, unrelated to locale. I get the following error: Which makes sense, because they're not the same, since on /app it's |
|
Locally this workaround works, but building keeps on failing because it's prerendering using backend calls with /default. |
|
The topic is pretty old but looks to be still alive so let me share very simple work around :) Calling page.tsx dictionary-config.ts: If you need the value for example in layout which has no access to query parameters then: middleware.ts: lauout.tsx: Remember to rename router structure from |
|
the better version for me |

Credit to @pecoram and @dgoerdes, we now have a solution for this in Next.js 12 with Middleware!