Intl-localematcher in next.js: RangeError: Incorrect locale information provided
Asked Answered
J

1

6

I am trying to add internationalisation to my next.js project using @formatjs/intl-localematcher. I added a config file with the following code:

// i18n.config.ts

export const i18n = {
  defaultLocale: "en",
  locales: ["en", "de", "fr"],
} as const;

export type Locale = (typeof i18n)["locales"][number];

Then, I added the following middleware:

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

import { i18n } from "@/i18n.config";

import { match as matchLocale } from "@formatjs/intl-localematcher";
import Negotiator from "negotiator";

function getLocale(request: NextRequest): string | undefined {
  const negotiatorHeaders: Record<string, string> = {};
  request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));

  // @ts-ignore locales are readonly
  const locales: string[] = i18n.locales;
  const languages = new Negotiator({ headers: negotiatorHeaders }).languages();

  const locale = matchLocale(languages, locales, i18n.defaultLocale);
  return locale;
}

export function middleware(request: NextRequest) {
  const pathname = request.nextUrl.pathname;
  const pathnameIsMissingLocale = i18n.locales.every(
    (locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
  );

  // Redirect if there is no locale
  if (pathnameIsMissingLocale) {
    const locale = getLocale(request);
    return NextResponse.redirect(
      new URL(
        `/${locale}${pathname.startsWith("/") ? "" : "/"}${pathname}`,
        request.url
      )
    );
  }
}

export const config = {
  // Matcher ignoring `/_next/` and `/api/`
  matcher: ["/((?!api|_next/static|_next/image).*)"],
};

When I deploy the project to vercel, I get the following error in the preview environment:

RangeError: Incorrect locale information provided
    at (node_modules/@formatjs/intl-localematcher/lib/abstract/CanonicalizeLocaleList.js:7:16)
    at (node_modules/@formatjs/intl-localematcher/lib/index.js:8:34)
    at (middleware.ts:18:4)
    at (middleware.ts:30:19)
    at (node_modules/next/dist/esm/server/web/adapter.js:163:22)
    at (node_modules/next/dist/esm/server/async-storage/request-async-storage-wrapper.js:72:23)
    at (node_modules/next/dist/esm/server/web/adapter.js:150:52)

The error seems to be originating from the following line:

const locale = matchLocale(languages, locales, i18n.defaultLocale);

However, given that I provided a default locale, I don't see why the above error occurs.

Jenellejenesia answered 14/10, 2023 at 8:45 Comment(0)
L
2

Had the same error with the match function from @formatjs/intl-localematcher. My solution was to wrap the match function in a try-catch and rely on the default locale for the error.

Change This:

const locale = matchLocale(languages, locales, i18n.defaultLocale);

To This:

  let locale = ''

  try {
    locale = matchLoacale(languages, locales, i18n.defaultLocale)
  } catch (error) {
    locale = i18n.defaultLocale
  }
Leonor answered 11/1 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.