akalongman / akalongman/laravel-multilang

Is it possible to use the `accept-languages` header to redirect to a locale instead of the config file?

Abierto
#24 0 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
PHP
Estrellas
54
Forks
15
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

On the initial redirect of the url `/` currently the MultiLang middleware uses the [first segment](https://github.com/akalongman/laravel-multilang/blob/383787cdf0cb01ad5586998007ac91ae36278a6c/src/MultiLang/MultiLang.php#L270) as the locale or uses the [fallback](https://github.com/akalongman/laravel-multilang/blob/383787cdf0cb01ad5586998007ac91ae36278a6c/src/MultiLang/MultiLang.php#L271) in the config file as the locale. Would it be possible try to use the `accept-languages` header first instead of going to fall back right away?

Something like (Ps: this is untested code):
```
$acceptLanguages = explode(';', $request->header('accept-languages');
$locale = null;
foreach($acceptLanguages as $lang) {
if(in_array($lang, array_keys(config('multilang.locales')))) {
$locale = $lang;
break;
}
}
// More code that sets the locale segment on the url and whatnot using the locale above
```
Between lines 271 and 272 of [Multilang.php](https://github.com/akalongman/laravel-multilang/blob/383787cdf0cb01ad5586998007ac91ae36278a6c/src/MultiLang/MultiLang.php) would make this a possibility I think.

Of course this could make a Denial of Service attack possible if someone passes a really long `accept-languages` header, so there is a need to check its length of something like that, but I belive this is better than just using the default locale on the config file.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.