import {Request} from "../../http/lifecycle/Request"; import {Injectable} from "../../di" import {Locale} from "../service/Locale"; import {HTTPKernelModule} from "../../http/kernel/HTTPKernelModule"; import {HTTPKernel} from "../../http/kernel/HTTPKernel"; import {InjectSessionHTTPModule} from "../../http/kernel/module/InjectSessionHTTPModule"; import {Session} from "../../http/session/Session"; /** * An HTTP kernel module that adds the Locale service to the request container. */ @Injectable() export class InjectRequestLocale extends HTTPKernelModule { public executeWithBlockingWriteback = true /** Register this kernel module to the given kernel. */ public static register(kernel: HTTPKernel) { kernel.register(this).after(InjectSessionHTTPModule) } /** * Gets or sets the default locale in the session and instantiates a Locale * service into the Request container based on said locale. * @param request */ public async apply(request: Request) { const session = request.make(Session) const locale = request.make(Locale) // Set the default locale in the session if ( !session.get('i18n.locale') ) { session.set('i18n.locale', locale.getDefaultLocale()) } locale.setLocale(session.get('i18n.locale')) request.registerSingletonInstance(Locale, locale) return request } }