فهرست منبع

select2多语言支持

jqh 5 سال پیش
والد
کامیت
6b2d26679f
3فایلهای تغییر یافته به همراه60 افزوده شده و 0 حذف شده
  1. 11 0
      resources/lang/zh-CN/select2.php
  2. 46 0
      src/Form/Field/Select.php
  3. 3 0
      src/Grid/Filter/Presenter/Select.php

+ 11 - 0
resources/lang/zh-CN/select2.php

@@ -0,0 +1,11 @@
+<?php
+
+return [
+    'error_loading'    => '无法载入结果',
+    'input_too_long'   => '请删除:num个字符',
+    'input_too_short'  => '请输入至少:num个字符',
+    'loading_more'     => '载入更多结果…',
+    'maximum_selected' => '最多只能选择:num个项目',
+    'no_results'       => '未找到结果',
+    'searching'        => '搜索中…',
+];

+ 46 - 0
src/Form/Field/Select.php

@@ -369,6 +369,8 @@ JS;
      */
     public function render()
     {
+        static::defineLang();
+
         $configs = array_merge([
             'allowClear'  => true,
             'placeholder' => [
@@ -400,4 +402,48 @@ JS;
 
         return parent::render();
     }
+
+    /**
+     * @return void
+     */
+    public static function defineLang()
+    {
+        $lang = trans('select2');
+        if (! is_array($lang) || empty($lang)) {
+            return;
+        }
+
+        $locale = config('app.locale');
+
+        Admin::script(
+            <<<JS
+(function () {
+    if (! $.fn.select2) {
+        return;
+    }
+    var e = $.fn.select2.amd;
+
+    return e.define("select2/i18n/{$locale}", [], function () {
+        return {
+            errorLoading: function () {
+                return "{$lang['error_loading']}"
+            }, inputTooLong: function (e) {
+                return "{$lang['input_too_long']}".replace(':num', e.input.length - e.maximum)
+            }, inputTooShort: function (e) {
+                return "{$lang['input_too_short']}".replace(':num', e.minimum - e.input.length)
+            }, loadingMore: function () {
+                return "{$lang['loading_more']}"
+            }, maximumSelected: function (e) {
+                return "{$lang['maximum_selected']}".replace(':num', e.maximum)
+            }, noResults: function () {
+                return "{$lang['no_results']}"
+            }, searching: function () {
+                 return "{$lang['searching']}"
+            }
+        }
+    }), {define: e.define, require: e.require}
+})()
+JS
+        );
+    }
 }

+ 3 - 0
src/Grid/Filter/Presenter/Select.php

@@ -2,6 +2,7 @@
 
 namespace Dcat\Admin\Grid\Filter\Presenter;
 
+use Dcat\Admin\Form\Field\Select as SelectForm;
 use Dcat\Admin\Admin;
 use Illuminate\Contracts\Support\Arrayable;
 use Illuminate\Database\Eloquent\Model;
@@ -51,6 +52,8 @@ class Select extends Presenter
     public function __construct($options)
     {
         $this->options = $options;
+
+        SelectForm::defineLang();
     }
 
     /**