🐛 Fix locale detection (#1631)

This commit is contained in:
Luke Vella 2025-03-14 11:23:46 +00:00 committed by GitHub
parent d826f57050
commit 7ff2661048
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 45 additions and 46 deletions

View file

@ -2,6 +2,15 @@
"name": "@rallly/languages",
"version": "0.0.0",
"private": true,
"main": "index.ts",
"types": "index.ts"
"exports": {
".": "./src/index.ts",
"./*": "./src/*.ts"
},
"dependencies": {
"@formatjs/intl-localematcher": "^0.6.0",
"negotiator": "^1.0.0"
},
"devDependencies": {
"@types/negotiator": "^0.6.3"
}
}

View file

@ -0,0 +1,17 @@
import languages, { defaultLocale } from "./index";
import Negotiator from "negotiator";
import { match } from "@formatjs/intl-localematcher";
import type { NextRequest } from "next/server";
const locales = Object.keys(languages);
export async function getPreferredLocale(req: NextRequest) {
const preferredLanguages = new Negotiator({
headers: {
"accept-language": req.headers.get("accept-language") ?? "",
},
}).languages();
const locale = match(preferredLanguages, locales, defaultLocale);
return locale;
}