mirror of
https://github.com/ArthurDanjou/artdanj-shortener.git
synced 2026-01-14 20:59:55 +01:00
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import I18n from '@ioc:Adonis/Addons/I18n'
|
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
|
|
|
/**
|
|
* The middleware detects the user language using the "Accept-language" HTTP header
|
|
* or the "lang" query string parameter.
|
|
*
|
|
* Feel free to change the middleware implementation to what suits your needs. Just
|
|
* make sure
|
|
*
|
|
* - You always ensure the user selected language is supported by your app.
|
|
* - Only call "switchLocale" when the detected language is valid string value and
|
|
* not "null" or "undefined"
|
|
*/
|
|
export default class DetectUserLocale {
|
|
/**
|
|
* Detect user language using "Accept-language" header or
|
|
* the "lang" query string parameter.
|
|
*
|
|
* The user language must be part of the "supportedLocales", otherwise
|
|
* this method should return null.
|
|
*/
|
|
protected getUserLanguage(ctx: HttpContextContract) {
|
|
const availableLocales = I18n.supportedLocales()
|
|
return ctx.request.language(availableLocales) || ctx.request.input('lang')
|
|
}
|
|
|
|
/**
|
|
* Handle method is called by AdonisJS automatically on every middleware
|
|
* class.
|
|
*/
|
|
public async handle(ctx: HttpContextContract, next: () => Promise<void>) {
|
|
const language = this.getUserLanguage(ctx)
|
|
|
|
/**
|
|
* Switch locale when we are able to detect the user language and it
|
|
* is supported by the application
|
|
*/
|
|
if (language) {
|
|
ctx.i18n.switchLocale(language)
|
|
}
|
|
|
|
await next()
|
|
}
|
|
}
|