瀏覽代碼

重构翻译功能,并增加 Admin::translation() 方法用于指定翻译文件路径

jqh 4 年之前
父節點
當前提交
dcbd91ac80
共有 5 個文件被更改,包括 125 次插入34 次删除
  1. 10 0
      src/Admin.php
  2. 2 0
      src/AdminServiceProvider.php
  3. 1 0
      src/Octane/Listeners/FlushAdminState.php
  4. 109 0
      src/Support/Translator.php
  5. 3 34
      src/Support/helpers.php

+ 10 - 0
src/Admin.php

@@ -126,6 +126,16 @@ class Admin
         static::context()->favicon = $favicon;
     }
 
+    /**
+     * 设置翻译文件路径.
+     *
+     * @param string|null $path
+     */
+    public static function translation(?string $path)
+    {
+        app('admin.translator')->setPath($path);
+    }
+
     /**
      * 获取登录用户模型.
      *

+ 2 - 0
src/AdminServiceProvider.php

@@ -14,6 +14,7 @@ use Dcat\Admin\Layout\Navbar;
 use Dcat\Admin\Layout\SectionManager;
 use Dcat\Admin\Support\Context;
 use Dcat\Admin\Support\Setting;
+use Dcat\Admin\Support\Translator;
 use Dcat\Admin\Support\WebUploader;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Facades\Blade;
@@ -233,6 +234,7 @@ class AdminServiceProvider extends ServiceProvider
         });
         $this->app->singleton('admin.web-uploader', WebUploader::class);
         $this->app->singleton(ExceptionHandler::class, config('admin.exception_handler') ?: Handler::class);
+        $this->app->singleton('admin.translator', Translator::class);
     }
 
     public function registerExtensions()

+ 1 - 0
src/Octane/Listeners/FlushAdminState.php

@@ -20,6 +20,7 @@ class FlushAdminState
         'admin.context',
         'admin.setting',
         'admin.web-uploader',
+        'admin.translator',
     ];
 
     protected $app;

+ 109 - 0
src/Support/Translator.php

@@ -0,0 +1,109 @@
+<?php
+
+namespace Dcat\Admin\Support;
+
+class Translator
+{
+    protected static $method;
+
+    /**
+     * @var \Illuminate\Contracts\Translation\Translator
+     */
+    protected $translator;
+
+    /**
+     * @var string
+     */
+    protected $path;
+
+    public function __construct()
+    {
+        $this->translator = app('translator');
+        $this->path = admin_controller_slug();
+    }
+
+    /**
+     * 设置翻译文件路径.
+     *
+     * @param null|string $path
+     *
+     * @return void
+     */
+    public function setPath(?string $path)
+    {
+        $this->path = $path;
+    }
+
+    /**
+     * 翻译字段名称.
+     *
+     * @param string $field
+     * @param null $locale
+     *
+     * @return false|mixed|string|string[]
+     */
+    public function transField(?string $field, $locale = null)
+    {
+        return $this->trans("{$this->path}.fields.{$field}", [], $locale);
+    }
+
+    /**
+     * 翻译Label.
+     *
+     * @param null|string $label
+     * @param array $replace
+     * @param string $locale
+     *
+     * @return array|\Illuminate\Contracts\Translation\Translator|string|null
+     */
+    public function transLabel(?string $label = null, $replace = [], $locale = null)
+    {
+        $label = $label ?: admin_controller_name();
+
+        return $this->trans("{$this->path}.labels.{$label}", $replace, $locale);
+    }
+
+    /**
+     * 翻译.
+     *
+     * @param string $key
+     * @param array $replace
+     * @param string $locale
+     *
+     * @return false|mixed|string|string[]
+     */
+    public function trans($key, array $replace = [], $locale = null)
+    {
+        $method = $this->getTranslateMethod();
+
+        if ($this->translator->has($key)) {
+            return $this->translator->$method($key, $replace, $locale);
+        }
+
+        if (
+            mb_strpos($key, 'global.') !== 0
+            && count($arr = explode('.', $key)) > 1
+        ) {
+            unset($arr[0]);
+            array_unshift($arr, 'global');
+            $key = implode('.', $arr);
+
+            if (! $this->translator->has($key)) {
+                return end($arr);
+            }
+
+            return $this->translator->$method($key, $replace, $locale);
+        }
+
+        return last(explode('.', $key));
+    }
+
+    protected function getTranslateMethod()
+    {
+        if (static::$method === null) {
+            static::$method = version_compare(app()->version(), '6.0', '>=') ? 'get' : 'trans';
+        }
+
+        return static::$method;
+    }
+}

+ 3 - 34
src/Support/helpers.php

@@ -168,9 +168,7 @@ if (! function_exists('admin_trans_field')) {
      */
     function admin_trans_field($field, $locale = null)
     {
-        $slug = admin_controller_slug();
-
-        return admin_trans("{$slug}.fields.{$field}", [], $locale);
+        return app('admin.translator')->transField($field, $locale);
     }
 }
 
@@ -186,10 +184,7 @@ if (! function_exists('admin_trans_label')) {
      */
     function admin_trans_label($label = null, $replace = [], $locale = null)
     {
-        $label = $label ?: admin_controller_name();
-        $slug = admin_controller_slug();
-
-        return admin_trans("{$slug}.labels.{$label}", $replace, $locale);
+        return app('admin.translator')->transLabel($label, $replace, $locale);
     }
 }
 
@@ -223,33 +218,7 @@ if (! function_exists('admin_trans')) {
      */
     function admin_trans($key, $replace = [], $locale = null)
     {
-        static $method = null;
-
-        if ($method === null) {
-            $method = version_compare(app()->version(), '6.0', '>=') ? 'get' : 'trans';
-        }
-
-        $translator = app('translator');
-
-        if ($translator->has($key)) {
-            return $translator->$method($key, $replace, $locale);
-        }
-        if (
-            mb_strpos($key, 'global.') !== 0
-            && count($arr = explode('.', $key)) > 1
-        ) {
-            unset($arr[0]);
-            array_unshift($arr, 'global');
-            $key = implode('.', $arr);
-
-            if (! $translator->has($key)) {
-                return end($arr);
-            }
-
-            return $translator->$method($key, $replace, $locale);
-        }
-
-        return last(explode('.', $key));
+        return app('admin.translator')->trans($key, $replace, $locale);
     }
 }