-
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. |
Beta Was this translation helpful? Give feedback.
Replies: 50 comments 134 replies
-
Ideal, this should be configurable with a boolean. |
Beta Was this translation helpful? Give feedback.
-
+1 for this request |
Beta Was this translation helpful? Give feedback.
-
FYI if there are more upvotes on the above discussion, then it'll increase the probability for it to be implemented. Source: #18582 |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
I made a PR with a fix for our Problem: #19035 |
Beta Was this translation helpful? Give feedback.
-
hello guys any news on this? Could be good to have this option and avoid the redirect solution. |
Beta Was this translation helpful? Give feedback.
-
Hi |
Beta Was this translation helpful? Give feedback.
-
We would like to have this too. |
Beta Was this translation helpful? Give feedback.
-
@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 :) |
Beta Was this translation helpful? Give feedback.
-
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
|
Beta Was this translation helpful? Give feedback.
-
need this |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
I would benefit from having this feature too. |
Beta Was this translation helpful? Give feedback.
-
Hi @timneutkens, Can you give an update on this? Can we wait for this feature or are you guys not going to implement it? |
Beta Was this translation helpful? Give feedback.
-
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
|
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
I do need such a feature |
Beta Was this translation helpful? Give feedback.
-
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; |
Beta Was this translation helpful? Give feedback.
-
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 :) |
Beta Was this translation helpful? Give feedback.
-
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
} |
Beta Was this translation helpful? Give feedback.
-
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) |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
If you're interested in handling this in the |
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
Locally this workaround works, but building keeps on failing because it's prerendering using backend calls with /default. |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
Who recommends that in the first place? |
Beta Was this translation helpful? Give feedback.
Credit to @pecoram and @dgoerdes, we now have a solution for this in Next.js 12 with Middleware!