Преглед на файлове

Merge branch 'detect_lang' of https://github.com/thierrytiti/converse.js into thierrytiti-detect_lang

JC Brand преди 10 години
родител
ревизия
1d0ffe34ab
променени са 3 файла, в които са добавени 30 реда и са изтрити 1 реда
  1. 26 1
      converse.js
  2. 1 0
      docs/CHANGES.rst
  3. 3 0
      docs/source/configuration.rst

+ 26 - 1
converse.js

@@ -215,7 +215,32 @@
 
         // Translation machinery
         // ---------------------
-        this.i18n = settings.i18n ? settings.i18n : locales.en;
+	this.isAvailableLocale=function(locale){
+            ret=null;
+            if (locales[locale]) ret=locales[locale];
+            else{
+                sublocale=locale.split("-")[0];
+                if (sublocale!=locale && locales[sublocale]) ret=locales[sublocale];
+            }
+            return ret;
+        };
+		
+        this.detectLocale=function(){
+            ret=null;
+            if (window.navigator.userLanguage) ret=this.isAvailableLocale(window.navigator.userLanguage);
+            else if (window.navigator.languages && !ret){
+                for (var i = 0; i < window.navigator.languages.length && !ret; i++) {
+                 ret=this.isAvailableLocale(window.navigator.languages[i]);
+                }
+            }
+            else if (window.navigator.browserLanguage && !ret) ret=this.isAvailableLocale(window.navigator.browserLanguage);
+            else if (window.navigator.language && !ret) ret=this.isAvailableLocale(window.navigator.language);
+            else if (window.navigator.systemLanguage && !ret) ret=this.isAvailableLocale(window.navigator.systemLanguage);
+            else {ret=locales.en}
+            return ret;
+        };
+
+        this.i18n = settings.i18n ? settings.i18n : this.detectLocale();
         var __ = $.proxy(utils.__, this);
         var ___ = utils.___;
 

+ 1 - 0
docs/CHANGES.rst

@@ -13,6 +13,7 @@ Changelog
 * Updated French translation [thierrytiti]
 * CSS fix: position and width of the div #conversejs [thierrytiti]
 * CSS fix: room-info bug on hover after room description loaded [thierrytiti]
+* I18N: Autodetection of User Locale if no i18n setting is set. [thierrytiti]
 
 0.9.3 (2015-05-01)
 ------------------

+ 3 - 0
docs/source/configuration.rst

@@ -360,6 +360,9 @@ If set to ``true``, then don't show offline users.
 i18n
 ----
 
+* Default:  Auto-detection of the User/Browser language
+
+If no locale is matching available locales, the default is ``en``.
 Specify the locale/language. The language must be in the ``locales`` object. Refer to
 ``./locale/locales.js`` to see which locales are supported.