UNPKG

@builder.io/qwik

Version:

An Open-Source sub-framework designed with a focus on server-side-rendering, lazy-loading, and styling/animation.

29 lines (27 loc) 891 B
// ... other imports import { guessLocale } from "compiled-i18n"; import type { RequestHandler } from "@builder.io/qwik-city"; /** * Handle incoming requests to determine and set the appropriate locale. * This function checks for a 'locale' query parameter, then a `locale` cookie, * and finally falls back to the 'Accept-Language' header. */ export const onRequest: RequestHandler = async ({ query, cookie, headers, locale, }) => { // Allow overriding locale with query param `locale` if (query.has("locale")) { const newLocale = guessLocale(query.get("locale")); cookie.delete("locale"); cookie.set("locale", newLocale, {}); locale(newLocale); } else { // Choose locale based on cookie or accept-language header const maybeLocale = cookie.get("locale")?.value || headers.get("accept-language"); locale(guessLocale(maybeLocale)); } };