Skip to content

Commit

Permalink
Set the locale based on a cookie
Browse files Browse the repository at this point in the history
This change sets the locale for the request based on a cookie. There's no way to set the cookie through the UI, and the only valid option is the DefaultLocale anyhow so it's safe to include without a feature flag.

Refs #2044
  • Loading branch information
thewilkybarkid committed Oct 25, 2024
1 parent 75a5384 commit 7a667ad
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/WebApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Cause, Config, Effect, flow, Layer, Option, pipe, Schema } from 'effect
import { Express, ExpressConfig, Locale, LoggedInUser } from './Context.js'
import { ExpressHttpApp } from './ExpressHttpApp.js'
import { expressServer } from './ExpressServer.js'
import { DefaultLocale } from './locales/index.js'
import { DefaultLocale, SupportedLocales } from './locales/index.js'
import { Router } from './Router.js'
import * as TemplatePage from './TemplatePage.js'
import { Uuid } from './types/index.js'
Expand Down Expand Up @@ -104,7 +104,11 @@ const getLoggedInUser = HttpMiddleware.make(app =>

const getLocale = HttpMiddleware.make(app =>
Effect.gen(function* () {
const locale = DefaultLocale
const locale = yield* pipe(
HttpServerRequest.schemaCookies(Schema.Struct({ locale: Schema.Literal(...SupportedLocales) })),
Effect.andThen(({ locale }) => locale),
Effect.orElseSucceed(() => DefaultLocale),
)

return yield* Effect.provideService(app, Locale, locale)
}),
Expand Down

0 comments on commit 7a667ad

Please sign in to comment.