-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from sinamics/auto_detect_lang
Autodetect language
- Loading branch information
Showing
7 changed files
with
77 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const supportedLocales = ["en", "no", "zh", "es"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,51 @@ | ||
export { default } from "next-auth/middleware"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { supportedLocales } from "./locales/lang"; | ||
export const config = { matcher: ["/dashboard", "/networks"] }; | ||
|
||
const PUBLIC_FILE = /\.(.*)$/; | ||
|
||
export async function middleware(req: NextRequest) { | ||
if ( | ||
req.nextUrl.pathname.startsWith("/_next") || | ||
req.nextUrl.pathname.includes("/api/") || | ||
PUBLIC_FILE.test(req.nextUrl.pathname) | ||
) { | ||
return; | ||
} | ||
|
||
// Handle automatic locale detection | ||
if (req.nextUrl.locale === "default") { | ||
const fallbackLocale = "en"; | ||
const acceptLanguageHeader = req.headers.get("accept-language"); | ||
let preferredLocale = fallbackLocale; | ||
|
||
if (acceptLanguageHeader) { | ||
// Parse the Accept-Language header and sort by quality score | ||
const locales = acceptLanguageHeader | ||
.split(",") | ||
.map((lang) => { | ||
const [locale, qValue] = lang.trim().split(";q="); | ||
return { | ||
locale: locale.split("-")[0], | ||
quality: qValue ? parseFloat(qValue) : 1, | ||
}; | ||
}) | ||
.sort((a, b) => b.quality - a.quality); // Sort based on quality values | ||
// Select the first supported locale with the highest quality score | ||
const matchedLocale = locales.find((l) => supportedLocales.includes(l.locale)); | ||
if (matchedLocale) { | ||
preferredLocale = matchedLocale.locale; | ||
} | ||
} | ||
|
||
// Redirect to the preferred locale if it's different from the current one | ||
if (preferredLocale !== req.nextUrl.locale) { | ||
return NextResponse.redirect( | ||
new URL( | ||
`/${preferredLocale}${req.nextUrl.pathname}${req.nextUrl.search}`, | ||
req.url, | ||
), | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters