Initial commit: fristy.net personal site
This commit is contained in:
51 files changed
+6052
No files matched your search
@@ -0,0 +1,48 @@
|
||||
export const LOCALES = ["tr", "en", "de"] as const;
|
||||
|
||||
export type Locale = (typeof LOCALES)[number];
|
||||
|
||||
export const DEFAULT_LOCALE: Locale = "tr";
|
||||
|
||||
export const LOCALE_COOKIE = "locale";
|
||||
|
||||
export const LOCALE_LABELS: Record<Locale, string> = {
|
||||
tr: "TR",
|
||||
en: "EN",
|
||||
de: "DE",
|
||||
};
|
||||
|
||||
export function isLocale(value: string): value is Locale {
|
||||
return (LOCALES as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
export function matchLocale(acceptLanguage: string | null): Locale {
|
||||
if (!acceptLanguage) {
|
||||
return DEFAULT_LOCALE;
|
||||
}
|
||||
const preferences = acceptLanguage
|
||||
.split(",")
|
||||
.map((part) => {
|
||||
const [range, ...params] = part.trim().split(";");
|
||||
let q = 1;
|
||||
for (const param of params) {
|
||||
const match = param.trim().match(/^q=([0-9.]+)$/);
|
||||
if (match) {
|
||||
q = Number(match[1]);
|
||||
}
|
||||
}
|
||||
return { range: range.toLowerCase(), q };
|
||||
})
|
||||
.sort((a, b) => b.q - a.q);
|
||||
|
||||
for (const { range } of preferences) {
|
||||
if (range === "*") {
|
||||
return DEFAULT_LOCALE;
|
||||
}
|
||||
const base = range.split("-")[0];
|
||||
if (isLocale(base)) {
|
||||
return base;
|
||||
}
|
||||
}
|
||||
return DEFAULT_LOCALE;
|
||||
}
|
||||
Reference in new issue
Block a user