SetLocale.php 1021 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\App;
  6. use Illuminate\Support\Facades\Session;
  7. class SetLocale
  8. {
  9. /**
  10. * Handle an incoming request.
  11. *
  12. * @param Request $request
  13. * @param Closure $next
  14. * @return mixed
  15. */
  16. public function handle($request, Closure $next)
  17. {
  18. if (Session::has('locale')) {
  19. $locale = Session::get('locale', config('SETTINGS::LOCALE:DEFAULT'));
  20. } else {
  21. if (config('SETTINGS::LOCALE:DYNAMIC') !== 'true') {
  22. $locale = config('SETTINGS::LOCALE:DEFAULT');
  23. } else {
  24. $locale = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);
  25. if (! in_array($locale, explode(',', config('SETTINGS::LOCALE:AVAILABLE')))) {
  26. $locale = config('SETTINGS::LOCALE:DEFAULT');
  27. }
  28. }
  29. }
  30. App::setLocale($locale);
  31. return $next($request);
  32. }
  33. }