Language.php 936 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Http\Middleware;
  11. use Carbon\Carbon;
  12. use Closure;
  13. use Illuminate\Auth\Access\AuthorizationException;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Support\Facades\App;
  16. class Language
  17. {
  18. /**
  19. * Handle an incoming request.
  20. *
  21. * @param Request $request
  22. * @param Closure $next
  23. * @param string|null $guard
  24. *
  25. * @return mixed
  26. *
  27. * @throws AuthorizationException
  28. */
  29. public function handle($request, Closure $next)
  30. {
  31. $language = $request->header('language');
  32. if (in_array($language, ['en', 'zh_CH']) && App::getLocale() != $language) {
  33. App::setLocale($language);
  34. }
  35. return $next($request);
  36. }
  37. }