akalongman / akalongman/laravel-multilang

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

Aberta
#24 0 comentários 0 reações 0 responsáveis Ver no GitHub
Linguagem predominante
PHP
Estrelas
54
Forks
15
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

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.

Guia de contribuição

Abrir o guia de contribuição

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.