LanguageController.php 867 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. class LanguageController extends Controller
  6. {
  7. public function index(Request $request)
  8. {
  9. return $this->switchLanguage($request);
  10. }
  11. /**
  12. * 切换语言并修改 app.locale
  13. *
  14. * @param Request $request
  15. * @return \Illuminate\Http\RedirectResponse
  16. */
  17. public function switchLanguage(Request $request)
  18. {
  19. // 从 URL 参数中获取语言,默认为 'en'
  20. $lang = $request->input('lang', 'en');
  21. if (!$lang)
  22. {
  23. abort(404);
  24. }
  25. // 验证是否是支持的语言
  26. if (!in_array($lang, ['en', 'zh_CN'])) {
  27. abort(404);
  28. }
  29. switchLanguage($lang);
  30. return response()->json(['success' => true, 'lang' => $lang]);
  31. }
  32. }