Files
website/app/src/routes/+layout.server.js
2025-07-24 17:18:00 +03:00

37 lines
1.0 KiB
JavaScript

import { redirect } from "@sveltejs/kit";
import { color } from "../lib/util.js";
// set the locale cookie
function set_locale(cookies, locale) {
cookies.set("locale", locale, { path: "/", secure: false, httpOnly: false });
}
export async function load({ cookies, request, url }) {
// if the locale param is specified, use the specified locale
let locale = url.searchParams.get("l");
if (locale) {
set_locale(cookies, locale);
return redirect(307, url.pathname);
}
// attempt get the preferred locale from cookies
locale = cookies.get("locale");
/* if that doesn't work, try the accept-language header, and update the
* cookies respectively */
if (!locale) {
locale = request.headers.get("accept-language")?.split(",")[0];
set_locale(cookies, locale);
}
return {
// the locale we detected
locale: locale,
/* color is randomly picked on the server and passed via data to the client
* so both the client and the server will have the same color */
color: color(),
};
}