mirror of
https://github.com/lukevella/rallly.git
synced 2025-08-06 09:59:00 +02:00
🐛 Fix locale detection (#1631)
This commit is contained in:
parent
d826f57050
commit
7ff2661048
11 changed files with 45 additions and 46 deletions
17
packages/languages/src/get-preferred-locale.ts
Normal file
17
packages/languages/src/get-preferred-locale.ts
Normal 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;
|
||||
}
|
7
packages/languages/src/index.ts
Normal file
7
packages/languages/src/index.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import languages from "./languages.json";
|
||||
|
||||
export const supportedLngs = Object.keys(languages);
|
||||
|
||||
export const defaultLocale = "en";
|
||||
|
||||
export default languages;
|
25
packages/languages/src/languages.json
Normal file
25
packages/languages/src/languages.json
Normal file
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"eu": "Basque",
|
||||
"ca": "Català",
|
||||
"cs": "Česky",
|
||||
"da": "Dansk",
|
||||
"de": "Deutsch",
|
||||
"en": "English",
|
||||
"en-GB": "English (UK)",
|
||||
"es": "Español",
|
||||
"fr": "Français",
|
||||
"hr": "Hrvatski",
|
||||
"it": "Italiano",
|
||||
"hu": "Magyar",
|
||||
"nl": "Nederlands",
|
||||
"no": "Norsk",
|
||||
"pl": "Polski",
|
||||
"pt-BR": "Português - Brasil",
|
||||
"ru": "Русский",
|
||||
"sk": "Slovenčina",
|
||||
"fi": "Suomi",
|
||||
"sv": "Svenska",
|
||||
"tr": "Türkçe",
|
||||
"zh": "简体中文",
|
||||
"zh-Hant": "繁體中文"
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue