瀏覽代碼

feat: 添加多语言支持

igb 6 月之前
父節點
當前提交
4c9c1cf5fe
共有 1 個文件被更改,包括 42 次插入0 次删除
  1. 42 0
      app/Admin/Controllers/LanguageController.php

+ 42 - 0
app/Admin/Controllers/LanguageController.php

@@ -0,0 +1,42 @@
+<?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 (!in_array($lang, ['en', 'zh_CN'])) {
+            return redirect()->back()->withErrors(['error' => '不支持的语言']);
+        }
+
+        $configArray = ['lang' => $lang,];
+
+        user_admin_config($configArray);
+
+        // 动态修改 app.locale 配置
+        config(['app.locale' => $lang]);
+
+//        // 返回重定向或其他操作
+//        return redirect()->back()->with('success', '语言已切换为: ' . $lang);
+        return redirect('/prime-control');
+    }
+
+}