瀏覽代碼

Improve the default language chain

If the language that is passed in through the options is just a
string, like "en-US" or "pt-BR", Select2 will now try to load the
base language along with the requested language and "en". So
requesting "pt-BR" will try to load "pt-BR", "pt", and "en".

This also catches errors and triggers a warning about not being
able to load the file. This should be a more user friendly error,
and it fixes the problem where Select2 would completely fail to
initialize if a language could not be found.

This closes https://github.com/select2/select2/issues/2934.
Kevin Brown 10 年之前
父節點
當前提交
1b46e8ab3e
共有 7 個文件被更改,包括 135 次插入20 次删除
  1. 27 4
      dist/js/select2.amd.full.js
  2. 27 4
      dist/js/select2.amd.js
  3. 27 4
      dist/js/select2.full.js
  4. 0 0
      dist/js/select2.full.min.js
  5. 27 4
      dist/js/select2.js
  6. 0 0
      dist/js/select2.min.js
  7. 27 4
      src/js/select2/defaults.js

+ 27 - 4
dist/js/select2.amd.full.js

@@ -3662,7 +3662,16 @@ define('select2/defaults',[
     }
 
     if (typeof options.language === 'string') {
-      options.language = [options.language];
+      // Check if the lanugage is specified with a region
+      if (options.language.indexOf('-') > 0) {
+        // Extract the region information if it is included
+        var languageParts = options.language.split('-');
+        var baseLanguage = languageParts[0];
+
+        options.language = [options.language, baseLanguage];
+      } else {
+        options.language = [options.language];
+      }
     }
 
     if ($.isArray(options.language)) {
@@ -3679,9 +3688,23 @@ define('select2/defaults',[
           // Try to load it with the original name
           language = Translation.loadPath(name);
         } catch (e) {
-          // If we couldn't load it, check if it wasn't the full path
-          name = this.defaults.amdLanguageBase + name;
-          language = Translation.loadPath(name);
+          try {
+            // If we couldn't load it, check if it wasn't the full path
+            name = this.defaults.amdLanguageBase + name;
+            language = Translation.loadPath(name);
+          } catch (ex) {
+            // The translation could not be loaded at all. Sometimes this is
+            // because of a configuration problem, other times this can be
+            // because of how Select2 helps load all possible translation files.
+            if (console && console.warn) {
+              console.warn(
+                'Select2: The lanugage file for "' + name + '" could not be ' +
+                'automatically loaded. A fallback will be used instead.'
+              );
+            }
+
+            continue;
+          }
         }
 
         languages.extend(language);

+ 27 - 4
dist/js/select2.amd.js

@@ -3662,7 +3662,16 @@ define('select2/defaults',[
     }
 
     if (typeof options.language === 'string') {
-      options.language = [options.language];
+      // Check if the lanugage is specified with a region
+      if (options.language.indexOf('-') > 0) {
+        // Extract the region information if it is included
+        var languageParts = options.language.split('-');
+        var baseLanguage = languageParts[0];
+
+        options.language = [options.language, baseLanguage];
+      } else {
+        options.language = [options.language];
+      }
     }
 
     if ($.isArray(options.language)) {
@@ -3679,9 +3688,23 @@ define('select2/defaults',[
           // Try to load it with the original name
           language = Translation.loadPath(name);
         } catch (e) {
-          // If we couldn't load it, check if it wasn't the full path
-          name = this.defaults.amdLanguageBase + name;
-          language = Translation.loadPath(name);
+          try {
+            // If we couldn't load it, check if it wasn't the full path
+            name = this.defaults.amdLanguageBase + name;
+            language = Translation.loadPath(name);
+          } catch (ex) {
+            // The translation could not be loaded at all. Sometimes this is
+            // because of a configuration problem, other times this can be
+            // because of how Select2 helps load all possible translation files.
+            if (console && console.warn) {
+              console.warn(
+                'Select2: The lanugage file for "' + name + '" could not be ' +
+                'automatically loaded. A fallback will be used instead.'
+              );
+            }
+
+            continue;
+          }
         }
 
         languages.extend(language);

+ 27 - 4
dist/js/select2.full.js

@@ -13197,7 +13197,16 @@ define('select2/defaults',[
     }
 
     if (typeof options.language === 'string') {
-      options.language = [options.language];
+      // Check if the lanugage is specified with a region
+      if (options.language.indexOf('-') > 0) {
+        // Extract the region information if it is included
+        var languageParts = options.language.split('-');
+        var baseLanguage = languageParts[0];
+
+        options.language = [options.language, baseLanguage];
+      } else {
+        options.language = [options.language];
+      }
     }
 
     if ($.isArray(options.language)) {
@@ -13214,9 +13223,23 @@ define('select2/defaults',[
           // Try to load it with the original name
           language = Translation.loadPath(name);
         } catch (e) {
-          // If we couldn't load it, check if it wasn't the full path
-          name = this.defaults.amdLanguageBase + name;
-          language = Translation.loadPath(name);
+          try {
+            // If we couldn't load it, check if it wasn't the full path
+            name = this.defaults.amdLanguageBase + name;
+            language = Translation.loadPath(name);
+          } catch (ex) {
+            // The translation could not be loaded at all. Sometimes this is
+            // because of a configuration problem, other times this can be
+            // because of how Select2 helps load all possible translation files.
+            if (console && console.warn) {
+              console.warn(
+                'Select2: The lanugage file for "' + name + '" could not be ' +
+                'automatically loaded. A fallback will be used instead.'
+              );
+            }
+
+            continue;
+          }
         }
 
         languages.extend(language);

文件差異過大導致無法顯示
+ 0 - 0
dist/js/select2.full.min.js


+ 27 - 4
dist/js/select2.js

@@ -4090,7 +4090,16 @@ define('select2/defaults',[
     }
 
     if (typeof options.language === 'string') {
-      options.language = [options.language];
+      // Check if the lanugage is specified with a region
+      if (options.language.indexOf('-') > 0) {
+        // Extract the region information if it is included
+        var languageParts = options.language.split('-');
+        var baseLanguage = languageParts[0];
+
+        options.language = [options.language, baseLanguage];
+      } else {
+        options.language = [options.language];
+      }
     }
 
     if ($.isArray(options.language)) {
@@ -4107,9 +4116,23 @@ define('select2/defaults',[
           // Try to load it with the original name
           language = Translation.loadPath(name);
         } catch (e) {
-          // If we couldn't load it, check if it wasn't the full path
-          name = this.defaults.amdLanguageBase + name;
-          language = Translation.loadPath(name);
+          try {
+            // If we couldn't load it, check if it wasn't the full path
+            name = this.defaults.amdLanguageBase + name;
+            language = Translation.loadPath(name);
+          } catch (ex) {
+            // The translation could not be loaded at all. Sometimes this is
+            // because of a configuration problem, other times this can be
+            // because of how Select2 helps load all possible translation files.
+            if (console && console.warn) {
+              console.warn(
+                'Select2: The lanugage file for "' + name + '" could not be ' +
+                'automatically loaded. A fallback will be used instead.'
+              );
+            }
+
+            continue;
+          }
         }
 
         languages.extend(language);

文件差異過大導致無法顯示
+ 0 - 0
dist/js/select2.min.js


+ 27 - 4
src/js/select2/defaults.js

@@ -195,7 +195,16 @@ define([
     }
 
     if (typeof options.language === 'string') {
-      options.language = [options.language];
+      // Check if the lanugage is specified with a region
+      if (options.language.indexOf('-') > 0) {
+        // Extract the region information if it is included
+        var languageParts = options.language.split('-');
+        var baseLanguage = languageParts[0];
+
+        options.language = [options.language, baseLanguage];
+      } else {
+        options.language = [options.language];
+      }
     }
 
     if ($.isArray(options.language)) {
@@ -212,9 +221,23 @@ define([
           // Try to load it with the original name
           language = Translation.loadPath(name);
         } catch (e) {
-          // If we couldn't load it, check if it wasn't the full path
-          name = this.defaults.amdLanguageBase + name;
-          language = Translation.loadPath(name);
+          try {
+            // If we couldn't load it, check if it wasn't the full path
+            name = this.defaults.amdLanguageBase + name;
+            language = Translation.loadPath(name);
+          } catch (ex) {
+            // The translation could not be loaded at all. Sometimes this is
+            // because of a configuration problem, other times this can be
+            // because of how Select2 helps load all possible translation files.
+            if (console && console.warn) {
+              console.warn(
+                'Select2: The lanugage file for "' + name + '" could not be ' +
+                'automatically loaded. A fallback will be used instead.'
+              );
+            }
+
+            continue;
+          }
         }
 
         languages.extend(language);

部分文件因文件數量過多而無法顯示