From 1deab0006d12874b1a8c80e12fece3045b248db7 Mon Sep 17 00:00:00 2001 From: nichenqin Date: Tue, 26 Nov 2024 11:25:52 +0800 Subject: [PATCH] chore: add i18n cookie --- apps/backend/src/modules/auth/auth.ts | 21 +++++++++++++++++++ .../blocks/member/member-menu.svelte | 1 + apps/frontend/src/routes/+layout.svelte | 11 ++++++++-- packages/i18n/src/client.ts | 4 ++++ packages/i18n/src/i18n/i18n-detector.ts | 3 ++- 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/apps/backend/src/modules/auth/auth.ts b/apps/backend/src/modules/auth/auth.ts index fec1120fd..c89f95855 100644 --- a/apps/backend/src/modules/auth/auth.ts +++ b/apps/backend/src/modules/auth/auth.ts @@ -446,6 +446,27 @@ export class Auth { response.headers.set("Set-Cookie", sessionCookie.serialize()) return response }) + .post( + "/api/lang/:lang", + (ctx) => { + const lang = ctx.params.lang + const cookie = new Cookie("lang", lang, { + path: "/", + maxAge: 365 * 24 * 60 * 60, + }) + return new Response(null, { + status: 200, + headers: { + "Set-Cookie": cookie.serialize(), + }, + }) + }, + { + params: t.Object({ + lang: t.Enum({ es: "es", en: "en", ko: "ko", ja: "ja", zh: "zh" }), + }), + }, + ) .post( "/api/reset-password", async (ctx) => { diff --git a/apps/frontend/src/lib/components/blocks/member/member-menu.svelte b/apps/frontend/src/lib/components/blocks/member/member-menu.svelte index 39fb9dd19..5911f971a 100644 --- a/apps/frontend/src/lib/components/blocks/member/member-menu.svelte +++ b/apps/frontend/src/lib/components/blocks/member/member-menu.svelte @@ -25,6 +25,7 @@ const changeLanguage = async (lang: Locales) => { await loadLocaleAsync(lang) + await fetch(`/api/lang/${lang}`, { method: "POST" }) setLocale(lang) localStorage.setItem("lang", lang) } diff --git a/apps/frontend/src/routes/+layout.svelte b/apps/frontend/src/routes/+layout.svelte index 4b5341b36..746008a49 100644 --- a/apps/frontend/src/routes/+layout.svelte +++ b/apps/frontend/src/routes/+layout.svelte @@ -7,7 +7,14 @@ import { QueryClient, QueryClientProvider } from "@tanstack/svelte-query" import ImagePreview from "$lib/components/blocks/attachment/image-preview.svelte" - import { setLocale, loadLocaleAsync, detectLocale, localStorageDetector } from "@undb/i18n/client" + import { + setLocale, + loadLocaleAsync, + detectLocale, + localStorageDetector, + queryStringDetector, + documentCookieDetector, + } from "@undb/i18n/client" import { onMount } from "svelte" const queryClient = new QueryClient({ defaultOptions: { @@ -18,7 +25,7 @@ }) onMount(async () => { - const lang = detectLocale(localStorageDetector) + const lang = detectLocale(documentCookieDetector, queryStringDetector, localStorageDetector) await loadLocaleAsync(lang) setLocale(lang) }) diff --git a/packages/i18n/src/client.ts b/packages/i18n/src/client.ts index 747a112c4..3a3c630cc 100644 --- a/packages/i18n/src/client.ts +++ b/packages/i18n/src/client.ts @@ -1,8 +1,12 @@ export * from "./i18n/en" +export * from "./i18n/es" export * from "./i18n/formatters" export * from "./i18n/i18n-detector" export * from "./i18n/i18n-svelte" export * from "./i18n/i18n-types" export * from "./i18n/i18n-util" export * from "./i18n/i18n-util.async" +export * from "./i18n/ja" +export * from "./i18n/ko" +export * from "./i18n/zh" diff --git a/packages/i18n/src/i18n/i18n-detector.ts b/packages/i18n/src/i18n/i18n-detector.ts index eaf4b3784..b0a35fdf5 100644 --- a/packages/i18n/src/i18n/i18n-detector.ts +++ b/packages/i18n/src/i18n/i18n-detector.ts @@ -1 +1,2 @@ -export { localStorageDetector } from 'typesafe-i18n/detectors'; +export { documentCookieDetector,localStorageDetector,queryStringDetector } from 'typesafe-i18n/detectors'; +