#How to set default locale to '/' not '/en'?

5 messages · Page 1 of 1 (latest)

austere jetty
#

hi.
I'm trying to make a multilingual website with app router.

I can't figure out how to not include it in the url for the 'en' language
For example now:
site.com/en

I want:
site.com (only for en lang)

I am using the following middleware

there is my try to do this, but this not working:
const newPathname = locale === 'en' ? pathname : /${locale}${pathname};

frank berryBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

austere jetty
#
let headers = { 'accept-language': 'en-US,en;q=0.5' };
let languages = new Negotiator({ headers }).languages();
let locales = ['en', 'de'];
let defaultLocale = 'en';

match(languages, locales, defaultLocale);

function getLocale(request) {
    // Extract the language from the request headers
    const acceptLanguage = request.headers.get('accept-language');

    // Parse the accept language header to get the preferred languages
    const preferredLanguages = acceptLanguage
        ? acceptLanguage.split(',').map((language) => {
              const [locale, q] = language.trim().split(';q=');
              return { locale, q: parseFloat(q || '1') };
          })
        : [];

    // Sort the preferred languages by quality (q) in descending order
    preferredLanguages.sort((a, b) => b.q - a.q);

    // Find the first supported language from the preferred languages
    const supportedLanguage = preferredLanguages.find((language) =>
        locales.includes(language.locale),
    );

    // Return the supported language or the default locale if none found
    return supportedLanguage ? supportedLanguage.locale : defaultLocale;
}

export function middleware(request) {
    // Check if there is any supported locale in the pathname
    const { pathname } = request.nextUrl;
    const pathnameHasLocale = locales.some(
        (locale) =>
            pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`,
    );

    if (pathnameHasLocale) return;

    // Redirect if there is no locale
    const locale = getLocale(request);
    const newPathname = locale === 'en' ? pathname : `/${locale}${pathname}`;
    request.nextUrl.pathname = newPathname;
    // e.g. incoming request is /products
    // The new URL is now /en/products if locale is not 'en'
    return Response.redirect(request.nextUrl);
}

export const config = {
    matcher: [
     (_next)
        '/((?!_next).*)',
        // Optional: only run on root (/) URL
        // '/'
    ],
};

stable delta
#

Hey, @austere jetty did you find a solution for this?

austere jetty