- Remove unused SupportedLocale import in results.tsx - Remove unused webroot variable in public/i18n.js - Configure knip.json to ignore i18n public API exports - Add language-selector and language-option to ESLint ignore list - Auto-fix Prettier formatting across all files - Fix better-tailwindcss line wrapping issues
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { Elysia } from "elysia";
|
|
import { type SupportedLocale, createTranslator, detectLocale, getLocale } from "./index";
|
|
|
|
/**
|
|
* Elysia plugin for i18n/locale handling
|
|
* Adds locale detection and translator to the context
|
|
*/
|
|
export const localeService = new Elysia({ name: "locale/service" }).derive(
|
|
{ as: "global" },
|
|
({ request }) => {
|
|
// Get locale from cookie (parsed from headers) or detect from Accept-Language header
|
|
const cookieHeader = request.headers.get("cookie") ?? "";
|
|
const acceptLanguage = request.headers.get("accept-language") ?? undefined;
|
|
|
|
// Parse locale from cookie string
|
|
let cookieLocale: string | undefined;
|
|
const localeMatch = cookieHeader.match(/locale=([^;]+)/);
|
|
if (localeMatch) {
|
|
cookieLocale = localeMatch[1];
|
|
}
|
|
|
|
let locale: SupportedLocale;
|
|
|
|
if (cookieLocale && typeof cookieLocale === "string") {
|
|
locale = getLocale(cookieLocale);
|
|
} else {
|
|
locale = detectLocale(acceptLanguage);
|
|
}
|
|
|
|
const translator = createTranslator(locale);
|
|
|
|
return {
|
|
locale,
|
|
t: translator,
|
|
};
|
|
},
|
|
);
|
|
|
|
export type LocaleContext = {
|
|
locale: SupportedLocale;
|
|
t: ReturnType<typeof createTranslator>;
|
|
};
|