SetLanguage.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Illuminate\Support\Facades\App;
  5. use App\Facades\Settings;
  6. class SetLanguage
  7. {
  8. /**
  9. * Handle an incoming request.
  10. *
  11. * @param \Illuminate\Http\Request $request
  12. * @param \Closure $next
  13. * @return mixed
  14. */
  15. public function handle($request, Closure $next)
  16. {
  17. // 3 possible cases here:
  18. // - The user has choosen a specific language among those available in the Setting view of 2FAuth
  19. // - The client send an accept-language header
  20. // - No language is passed from the client
  21. //
  22. // We prioritize the user defined one, then the request header one, and finally the fallback one.
  23. // FI: Settings::get() always returns a fallback value
  24. $lang = Settings::get('lang');
  25. if($lang === 'browser') {
  26. $lang = config('app.fallback_locale');
  27. if ($request->hasHeader("Accept-Language")) {
  28. // We only keep the primary language passed via the header.
  29. $lang = head(explode(',', $request->header("Accept-Language")));
  30. }
  31. }
  32. // If the language is not available (or partial), strings will be translated using the fallback language.
  33. App::setLocale($lang);
  34. return $next($request);
  35. }
  36. }