first commit
This commit is contained in:
32
i18n/request.js
Normal file
32
i18n/request.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import { cookies, headers } from "next/headers";
|
||||
import { getRequestConfig } from "next-intl/server";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
export default getRequestConfig(async () => {
|
||||
const cookieStore = await cookies();
|
||||
|
||||
const dictionaryPath = path.join(process.cwd(), "dictionary");
|
||||
const supportedLocales = fs.readdirSync(dictionaryPath)
|
||||
.filter(file => file.endsWith(".json"))
|
||||
.map(file => file.replace(".json", ""));
|
||||
|
||||
let locale = cookieStore.get("locale")?.value;
|
||||
|
||||
if (!locale) {
|
||||
const headersList = await headers();
|
||||
const acceptLanguage = headersList.get("accept-language");
|
||||
const browserLang = acceptLanguage?.split(",")[0]?.split("-")[0] || "en";
|
||||
|
||||
locale = supportedLocales.includes(browserLang) ? browserLang : "en";
|
||||
}
|
||||
|
||||
if (!supportedLocales.includes(locale)) {
|
||||
locale = "en";
|
||||
}
|
||||
|
||||
return {
|
||||
locale,
|
||||
messages: (await import(`../dictionary/${locale}.json`)).default
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user