Browse Source

feat: 添加多语言支持

igb 6 months ago
parent
commit
63f62cca94
4 changed files with 43 additions and 1 deletions
  1. 4 0
      app/Admin/bootstrap.php
  2. 2 0
      app/Admin/routes.php
  3. 2 1
      app/Providers/AppServiceProvider.php
  4. 35 0
      app/helpers.php

+ 4 - 0
app/Admin/bootstrap.php

@@ -24,3 +24,7 @@ use Dcat\Admin\Show;
  * Admin::js('/packages/prettydocs/js/main.js');
  *
  */
+
+// 覆盖默认配置
+config(['admin' => user_admin_config()]);
+config(['app.locale' => config('admin.lang') ?: config('app.locale')]);

+ 2 - 0
app/Admin/routes.php

@@ -23,4 +23,6 @@ Route::group([
     $router->resource('product-parameter', 'BaseProductParameterController');
     //视频分类
     $router->resource('video-category', 'BaseVideoCategoryController');
+
+    $router->resource('language', 'LanguageController');
 });

+ 2 - 1
app/Providers/AppServiceProvider.php

@@ -13,7 +13,8 @@ class AppServiceProvider extends ServiceProvider
      */
     public function register()
     {
-        //
+        //添加自定义辅助函数
+        require_once __DIR__.'/../helpers.php';
     }
 
     /**

+ 35 - 0
app/helpers.php

@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Support\Arr;
+
+if (! function_exists('user_admin_config')) {
+    function user_admin_config($key = null, $value = null)
+    {
+        // 获取 session 实例
+        $session = session();
+
+        // 从 session 中获取 'admin.config',如果没有则使用默认的 'admin' 配置
+        $config = $session->get('admin.config', function () {
+            $adminConfig = config('admin');
+            $adminConfig['lang'] = config('app.locale');
+            return $adminConfig;
+        });
+
+        // 如果 $key 是数组,表示我们需要批量设置配置项
+        if (is_array($key)) {
+            foreach ($key as $k => $v) {
+                Arr::set($config, $k, $v); // 在配置数组中设置每个键值对
+            }
+            $session->put('admin.config', $config); // 将更新后的配置保存到 session 中
+            return;
+        }
+
+        // 如果没有传递具体的 key,返回整个配置数组
+        if (is_null($key)) {
+            return $config;
+        }
+
+        // 获取指定的配置项,如果不存在则返回默认值 $value
+        return Arr::get($config, $key, $value);
+    }
+}