Skip to content

Commit

Permalink
fix: Update @formatjs/intl-localematcher to latest version (#1140)
Browse files Browse the repository at this point in the history
Includes updates to the distance algorithm for matching locales that is
used in the `next-intl` middleware.
  • Loading branch information
amannn authored Aug 20, 2024
1 parent 45668b0 commit c217582
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 59 deletions.
4 changes: 2 additions & 2 deletions packages/next-intl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"next.js"
],
"dependencies": {
"@formatjs/intl-localematcher": "^0.2.32",
"@formatjs/intl-localematcher": "^0.5.4",
"negotiator": "^0.6.3",
"use-intl": "workspace:^"
},
Expand Down Expand Up @@ -144,7 +144,7 @@
},
{
"path": "dist/production/middleware.js",
"limit": "6.525 KB"
"limit": "11.515 KB"
},
{
"path": "dist/production/routing.js",
Expand Down
5 changes: 3 additions & 2 deletions packages/next-intl/src/middleware/resolveLocale.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('getAcceptLanguageLocale', () => {
).toBe('en');
});

it('resolves a more generic locale to a specific one', () => {
it('resolves a locale with a different region to one that matches at least the language', () => {
const requestHeaders = new Headers({
'accept-language': 'en-GB;q=0.9'
});
Expand All @@ -24,7 +24,8 @@ describe('getAcceptLanguageLocale', () => {
).toBe('en-US');
});

it('resolves the most specific locale', () => {
it('resolves the most specific locale, even if it is defined after a more generic one', () => {
// Related to https://github.com/formatjs/formatjs/issues/4469
const requestHeaders = new Headers({
'accept-language': 'de-DE,de;q=0.9,en-US;q=0.8'
});
Expand Down
9 changes: 8 additions & 1 deletion packages/next-intl/src/middleware/resolveLocale.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ function findDomainFromHost<AppLocales extends Locales>(
return undefined;
}

function orderLocales<AppLocales extends Locales>(locales: AppLocales) {
// Workaround for https://github.com/formatjs/formatjs/issues/4469
return locales.slice().sort((a, b) => b.length - a.length);
}

export function getAcceptLanguageLocale<AppLocales extends Locales>(
requestHeaders: Headers,
locales: AppLocales,
Expand All @@ -40,9 +45,11 @@ export function getAcceptLanguageLocale<AppLocales extends Locales>(
}
}).languages();
try {
const orderedLocales = orderLocales(locales);

locale = match(
languages,
locales as unknown as Array<string>,
orderedLocales as unknown as Array<string>,
defaultLocale
);
} catch (e) {
Expand Down
79 changes: 25 additions & 54 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c217582

Please sign in to comment.