Sfoglia il codice sorgente

异步加载默认读取当前控制器的翻译文件

jqh 4 anni fa
parent
commit
7c6b675398

+ 13 - 1
src/Admin.php

@@ -133,7 +133,7 @@ class Admin
      */
     public static function translation(?string $path)
     {
-        app('admin.translator')->setPath($path);
+        static::context()->translation = $path;
     }
 
     /**
@@ -338,6 +338,8 @@ class Admin
     }
 
     /**
+     * 上下文管理.
+     *
      * @return \Dcat\Admin\Support\Context
      */
     public static function context()
@@ -345,6 +347,16 @@ class Admin
         return app('admin.context');
     }
 
+    /**
+     * 翻译器.
+     *
+     * @return \Dcat\Admin\Support\Translator
+     */
+    public static function translator()
+    {
+        return app('admin.translator');
+    }
+
     /**
      * @param array|string $name
      *

+ 21 - 0
src/Http/Controllers/AdminController.php

@@ -26,6 +26,13 @@ class AdminController extends Controller
         //        'create' => 'Create',
     ];
 
+    /**
+     * Set translation path.
+     *
+     * @var string
+     */
+    protected $translation;
+
     /**
      * Get content title.
      *
@@ -46,6 +53,16 @@ class AdminController extends Controller
         return $this->description;
     }
 
+    /**
+     * Get translation path.
+     *
+     * @return string
+     */
+    protected function translation()
+    {
+        return $this->translation;
+    }
+
     /**
      * Index interface.
      *
@@ -56,6 +73,7 @@ class AdminController extends Controller
     public function index(Content $content)
     {
         return $content
+            ->translation($this->translation())
             ->title($this->title())
             ->description($this->description()['index'] ?? trans('admin.list'))
             ->body($this->grid());
@@ -72,6 +90,7 @@ class AdminController extends Controller
     public function show($id, Content $content)
     {
         return $content
+            ->translation($this->translation())
             ->title($this->title())
             ->description($this->description()['show'] ?? trans('admin.show'))
             ->body($this->detail($id));
@@ -88,6 +107,7 @@ class AdminController extends Controller
     public function edit($id, Content $content)
     {
         return $content
+            ->translation($this->translation())
             ->title($this->title())
             ->description($this->description()['edit'] ?? trans('admin.edit'))
             ->body($this->form()->edit($id));
@@ -103,6 +123,7 @@ class AdminController extends Controller
     public function create(Content $content)
     {
         return $content
+            ->translation($this->translation())
             ->title($this->title())
             ->description($this->description()['create'] ?? trans('admin.create'))
             ->body($this->form());

+ 9 - 5
src/Http/Controllers/RenderableController.php

@@ -9,13 +9,10 @@ use Illuminate\Http\Request;
 
 class RenderableController
 {
-    /**
-     * @param Request $request
-     *
-     * @return mixed|string
-     */
     public function handle(Request $request)
     {
+        $this->initTranslation($request);
+
         $renderable = $this->newRenderable($request);
 
         $this->addScript();
@@ -37,6 +34,13 @@ class RenderableController
             .$asset->styleToHtml();
     }
 
+    protected function initTranslation(Request $request)
+    {
+        if ($path = $request->get('_trans_')) {
+            Admin::translation($path);
+        }
+    }
+
     protected function newRenderable(Request $request): LazyRenderable
     {
         $class = $request->get('renderable');

+ 14 - 0
src/Layout/Content.php

@@ -119,6 +119,20 @@ class Content implements Renderable
         return $this;
     }
 
+    /**
+     * 设置翻译文件路径.
+     *
+     * @param string|null $translation
+     *
+     * @return $this
+     */
+    public function translation(?string $translation)
+    {
+        Admin::translation($translation);
+
+        return $this;
+    }
+
     /**
      * Build full page.
      *

+ 12 - 0
src/Support/Context.php

@@ -14,6 +14,7 @@ use Illuminate\Support\Fluent;
  * @property array|null $html
  * @property array|null $ignoreQueries
  * @property array|null $jsVariables
+ * @property string $translation
  */
 class Context extends Fluent
 {
@@ -33,6 +34,17 @@ class Context extends Fluent
         return Arr::get($this->attributes, $key, $default);
     }
 
+    public function remember($key, \Closure $callback)
+    {
+        if (($value = $this->get($key)) !== null) {
+            return $value;
+        }
+
+        return tap($callback(), function ($value) use ($key) {
+            $this->set($key, $value);
+        });
+    }
+
     public function getArray($key, $default = null)
     {
         return Helper::array($this->get($key, $default));

+ 16 - 3
src/Support/Translator.php

@@ -2,6 +2,8 @@
 
 namespace Dcat\Admin\Support;
 
+use Dcat\Admin\Admin;
+
 class Translator
 {
     protected static $method;
@@ -19,7 +21,6 @@ class Translator
     public function __construct()
     {
         $this->translator = app('translator');
-        $this->path = admin_controller_slug();
     }
 
     /**
@@ -34,6 +35,18 @@ class Translator
         $this->path = $path;
     }
 
+    /**
+     * @return string
+     */
+    public function getPath()
+    {
+        if (! $this->path) {
+            $this->path = Admin::context()->translation ?: admin_controller_slug();
+        }
+
+        return $this->path;
+    }
+
     /**
      * 翻译字段名称.
      *
@@ -44,7 +57,7 @@ class Translator
      */
     public function transField(?string $field, $locale = null)
     {
-        return $this->trans("{$this->path}.fields.{$field}", [], $locale);
+        return $this->trans("{$this->getPath()}.fields.{$field}", [], $locale);
     }
 
     /**
@@ -60,7 +73,7 @@ class Translator
     {
         $label = $label ?: admin_controller_name();
 
-        return $this->trans("{$this->path}.labels.{$label}", $replace, $locale);
+        return $this->trans("{$this->getPath()}.labels.{$label}", $replace, $locale);
     }
 
     /**

+ 15 - 0
src/Traits/LazyWidget.php

@@ -2,6 +2,15 @@
 
 namespace Dcat\Admin\Traits;
 
+use Dcat\Admin\Admin;
+
+/**
+ * Trait LazyWidget
+ *
+ * @package Dcat\Admin\Traits
+ *
+ * @property string $translation
+ */
 trait LazyWidget
 {
     protected $payload = [];
@@ -13,10 +22,16 @@ trait LazyWidget
         return $this;
     }
 
+    public function translation()
+    {
+        return empty($this->translation) ? Admin::translator()->getPath() : $this->translation;
+    }
+
     public function getUrl()
     {
         $data = array_merge($this->payload, [
             'renderable' => $this->getRenderableName(),
+            '_trans_'    => $this->translation(),
         ]);
 
         return route(admin_api_route_name('render'), $data);