1234567891011121314151617181920212223242526272829303132333435363738 |
- <?php
- namespace App\Admin\Controllers;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- class LanguageController extends Controller
- {
- public function index(Request $request)
- {
- return $this->switchLanguage($request);
- }
- /**
- * 切换语言并修改 app.locale
- *
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function switchLanguage(Request $request)
- {
- // 从 URL 参数中获取语言,默认为 'en'
- $lang = $request->input('lang', 'en');
- if (!$lang)
- {
- abort(404);
- }
- // 验证是否是支持的语言
- if (!in_array($lang, ['en', 'zh_CN'])) {
- abort(404);
- }
- switchLanguage($lang);
- return response()->json(['success' => true, 'lang' => $lang]);
- }
- }
|