2
0
Эх сурвалжийг харах

Move some utility methods to utils.js

JC Brand 9 жил өмнө
parent
commit
0c393cf286
2 өөрчлөгдсөн 46 нэмэгдсэн , 46 устгасан
  1. 2 46
      src/converse-core.js
  2. 44 0
      src/utils.js

+ 2 - 46
src/converse-core.js

@@ -197,55 +197,11 @@
             };
         };
 
-        this.isLocaleAvailable = function (locale, available) {
-            /* Check whether the locale or sub locale (e.g. en-US, en) is supported.
-             *
-             * Parameters:
-             *      (Function) available - returns a boolean indicating whether the locale is supported
-             */
-            if (available(locale)) {
-                return locale;
-            } else {
-                var sublocale = locale.split("-")[0];
-                if (sublocale !== locale && available(sublocale)) {
-                    return sublocale;
-                }
-            }
-        };
-
-        this.detectLocale = function (library_check) {
-            /* Determine which locale is supported by the user's system as well
-             * as by the relevant library (e.g. converse.js or moment.js).
-             *
-             * Parameters:
-             *      (Function) library_check - returns a boolean indicating whether the locale is supported
-             */
-            var locale, i;
-            if (window.navigator.userLanguage) {
-                locale = this.isLocaleAvailable(window.navigator.userLanguage, library_check);
-            }
-            if (window.navigator.languages && !locale) {
-                for (i=0; i<window.navigator.languages.length && !locale; i++) {
-                    locale = this.isLocaleAvailable(window.navigator.languages[i], library_check);
-                }
-            }
-            if (window.navigator.browserLanguage && !locale) {
-                locale = this.isLocaleAvailable(window.navigator.browserLanguage, library_check);
-            }
-            if (window.navigator.language && !locale) {
-                locale = this.isLocaleAvailable(window.navigator.language, library_check);
-            }
-            if (window.navigator.systemLanguage && !locale) {
-                locale = this.isLocaleAvailable(window.navigator.systemLanguage, library_check);
-            }
-            return locale || 'en';
-        };
-
         if (!moment.locale) { //moment.lang is deprecated after 2.8.1, use moment.locale instead
             moment.locale = moment.lang;
         }
-        moment.locale(this.detectLocale(this.isMomentLocale));
-        this.i18n = settings.i18n ? settings.i18n : locales[this.detectLocale(this.isConverseLocale)] || {};
+        moment.locale(utils.detectLocale(this.isMomentLocale));
+        this.i18n = settings.i18n ? settings.i18n : locales[utils.detectLocale(this.isConverseLocale)] || {};
 
         // Translation machinery
         // ---------------------

+ 44 - 0
src/utils.js

@@ -149,6 +149,50 @@
             return str;
         },
 
+        isLocaleAvailable: function (locale, available) {
+            /* Check whether the locale or sub locale (e.g. en-US, en) is supported.
+             *
+             * Parameters:
+             *      (Function) available - returns a boolean indicating whether the locale is supported
+             */
+            if (available(locale)) {
+                return locale;
+            } else {
+                var sublocale = locale.split("-")[0];
+                if (sublocale !== locale && available(sublocale)) {
+                    return sublocale;
+                }
+            }
+        },
+
+        detectLocale: function (library_check) {
+            /* Determine which locale is supported by the user's system as well
+             * as by the relevant library (e.g. converse.js or moment.js).
+             *
+             * Parameters:
+             *      (Function) library_check - returns a boolean indicating whether the locale is supported
+             */
+            var locale, i;
+            if (window.navigator.userLanguage) {
+                locale = utils.isLocaleAvailable(window.navigator.userLanguage, library_check);
+            }
+            if (window.navigator.languages && !locale) {
+                for (i=0; i<window.navigator.languages.length && !locale; i++) {
+                    locale = utils.isLocaleAvailable(window.navigator.languages[i], library_check);
+                }
+            }
+            if (window.navigator.browserLanguage && !locale) {
+                locale = utils.isLocaleAvailable(window.navigator.browserLanguage, library_check);
+            }
+            if (window.navigator.language && !locale) {
+                locale = utils.isLocaleAvailable(window.navigator.language, library_check);
+            }
+            if (window.navigator.systemLanguage && !locale) {
+                locale = utils.isLocaleAvailable(window.navigator.systemLanguage, library_check);
+            }
+            return locale || 'en';
+        },
+
         isOTRMessage: function (message) {
             var $body = $(message).children('body'),
                 text = ($body.length > 0 ? $body.text() : undefined);