17 lines
474 B
TypeScript
17 lines
474 B
TypeScript
import type { GetServerSidePropsContext } from "next"
|
|
import url from "url"
|
|
|
|
export const languageDetector = (ctx: GetServerSidePropsContext) => {
|
|
const queryLang = url.parse(ctx.req.url!, true).query.lang as string
|
|
|
|
let acceptLang = ctx.req.headers["accept-language"]?.split(",")[0]
|
|
|
|
if (acceptLang === "zh-HK" || acceptLang === "zh-TW") {
|
|
acceptLang = "zh-TW"
|
|
} else {
|
|
acceptLang = acceptLang?.split("-")[0]
|
|
}
|
|
|
|
return queryLang || acceptLang || "en"
|
|
}
|