Skip to content

Commit

Permalink
chore: add i18n cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Nov 26, 2024
1 parent df9fc41 commit 1deab00
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
21 changes: 21 additions & 0 deletions apps/backend/src/modules/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 9 additions & 2 deletions apps/frontend/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -18,7 +25,7 @@
})
onMount(async () => {
const lang = detectLocale(localStorageDetector)
const lang = detectLocale(documentCookieDetector, queryStringDetector, localStorageDetector)
await loadLocaleAsync(lang)
setLocale(lang)
})
Expand Down
4 changes: 4 additions & 0 deletions packages/i18n/src/client.ts
Original file line number Diff line number Diff line change
@@ -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"

3 changes: 2 additions & 1 deletion packages/i18n/src/i18n/i18n-detector.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { localStorageDetector } from 'typesafe-i18n/detectors';
export { documentCookieDetector,localStorageDetector,queryStringDetector } from 'typesafe-i18n/detectors';

0 comments on commit 1deab00

Please sign in to comment.