SetLocale.php 1.0 KB

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