Import other modules into monorepo
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
40
src/i18n/modules/InjectRequestLocale.ts
Normal file
40
src/i18n/modules/InjectRequestLocale.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
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 = <Session> request.make(Session)
|
||||
const locale = <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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user