Standard libraries that lift up your code.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

40 lines
1.4 KiB

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): void {
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): Promise<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
}
}