having problem with i18n and locale in the router
Closed this issue · 5 comments
i have problem in initializing the i18n in the router
the problem is that the router ignore the defaultLocale defined in the config file and always translate my website based on 'en' locale
const i18nConfig = {
languageList: null,
locales: ['fa', 'en', 'tr'],
defaultLocale: 'fa',
localeDetection: false,
};
module.exports = i18nConfig;
this is my locale
but initialy always my app starts with
http://localhost:3000/en
also i represent my middleware if anyone has any idea that why always 'en' is settled up in my router !
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import i18nConfig from './i18nConfig';
import { i18nRouter } from 'next-i18n-router';
export async function middleware(request: NextRequest) {
const path = request.nextUrl.pathname;
const token = request.cookies.get('access_token')?.value || '';
const isPublicPath = path === '/' || i18nConfig.locales.includes(path.slice(1));
const PUBLIC_FILE = /\.(.*)$/;
if (
path.startsWith('/_next') || // exclude Next.js internals
path.startsWith('/api') || // exclude all API routes
path.startsWith('/static') || // exclude static files
PUBLIC_FILE.test(path) // exclude all files in the public folder
) {
return NextResponse.next();
}
if (!isPublicPath && !token) {
return NextResponse.redirect(new URL('/', request.nextUrl));
}
if (isPublicPath && token) {
return NextResponse.redirect(new URL('/bpms/dashboard', request.nextUrl));
}
return i18nRouter(request, i18nConfig);
}
It's likely that your browser language is set to English. Therefore the middleware is redirecting you to your preferred language. To view a different language, you can either:
- Set the
serverSetCookie
config option - Set the NEXT_LOCALE cookie to the locale language you want to view.
We get this confusion a lot, so we're very likely going to change the default behavior of serverSetCookie
to "always"
in the next version.
i add serverSetCookie: 'always',
also to my config object
yes the NEXT_LOCALE is setting in the cookie but this is again setting the 'en' language
I'm not quite understanding the problem. Is the problem that when you visit "/" it redirects to "/en"? If that's the case, then it sounds like you want to disable automatic locale detection. Change localeDetector
to false
.
localeDetector yes! i missunderstand this key with localeDetection!
my bad
thank you ; as always :)
Ah, yeah I see that error in your code now. Glad we figured it out!