Skip to content

Commit 1deab00

Browse files
committed
chore: add i18n cookie
1 parent df9fc41 commit 1deab00

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

apps/backend/src/modules/auth/auth.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,27 @@ export class Auth {
446446
response.headers.set("Set-Cookie", sessionCookie.serialize())
447447
return response
448448
})
449+
.post(
450+
"/api/lang/:lang",
451+
(ctx) => {
452+
const lang = ctx.params.lang
453+
const cookie = new Cookie("lang", lang, {
454+
path: "/",
455+
maxAge: 365 * 24 * 60 * 60,
456+
})
457+
return new Response(null, {
458+
status: 200,
459+
headers: {
460+
"Set-Cookie": cookie.serialize(),
461+
},
462+
})
463+
},
464+
{
465+
params: t.Object({
466+
lang: t.Enum({ es: "es", en: "en", ko: "ko", ja: "ja", zh: "zh" }),
467+
}),
468+
},
469+
)
449470
.post(
450471
"/api/reset-password",
451472
async (ctx) => {

apps/frontend/src/lib/components/blocks/member/member-menu.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
2626
const changeLanguage = async (lang: Locales) => {
2727
await loadLocaleAsync(lang)
28+
await fetch(`/api/lang/${lang}`, { method: "POST" })
2829
setLocale(lang)
2930
localStorage.setItem("lang", lang)
3031
}

apps/frontend/src/routes/+layout.svelte

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
88
import { QueryClient, QueryClientProvider } from "@tanstack/svelte-query"
99
import ImagePreview from "$lib/components/blocks/attachment/image-preview.svelte"
10-
import { setLocale, loadLocaleAsync, detectLocale, localStorageDetector } from "@undb/i18n/client"
10+
import {
11+
setLocale,
12+
loadLocaleAsync,
13+
detectLocale,
14+
localStorageDetector,
15+
queryStringDetector,
16+
documentCookieDetector,
17+
} from "@undb/i18n/client"
1118
import { onMount } from "svelte"
1219
const queryClient = new QueryClient({
1320
defaultOptions: {
@@ -18,7 +25,7 @@
1825
})
1926
2027
onMount(async () => {
21-
const lang = detectLocale(localStorageDetector)
28+
const lang = detectLocale(documentCookieDetector, queryStringDetector, localStorageDetector)
2229
await loadLocaleAsync(lang)
2330
setLocale(lang)
2431
})

packages/i18n/src/client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
export * from "./i18n/en"
2+
export * from "./i18n/es"
23
export * from "./i18n/formatters"
34
export * from "./i18n/i18n-detector"
45
export * from "./i18n/i18n-svelte"
56
export * from "./i18n/i18n-types"
67
export * from "./i18n/i18n-util"
78
export * from "./i18n/i18n-util.async"
9+
export * from "./i18n/ja"
10+
export * from "./i18n/ko"
11+
export * from "./i18n/zh"
812

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export { localStorageDetector } from 'typesafe-i18n/detectors';
1+
export { documentCookieDetector,localStorageDetector,queryStringDetector } from 'typesafe-i18n/detectors';
2+

0 commit comments

Comments
 (0)