summaryrefslogtreecommitdiff
path: root/lib/dojo/i18n.js
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2011-03-04 19:02:28 +0300
committerAndrew Dolgov <[email protected]>2011-03-04 19:02:59 +0300
commita089699c8915636ba4f158d77dba9b012bc93208 (patch)
treeb2d7d051f1f55d44a6be07d3ee137e5a7ccfcefb /lib/dojo/i18n.js
parentcfad9259a6feacfa8194b1312770ae6db1ecce50 (diff)
build custom layer of Dojo to speed up loading of tt-rss (refs #293)
Diffstat (limited to 'lib/dojo/i18n.js')
-rw-r--r--lib/dojo/i18n.js395
1 files changed, 241 insertions, 154 deletions
diff --git a/lib/dojo/i18n.js b/lib/dojo/i18n.js
index e914392a1..d06a8f25e 100644
--- a/lib/dojo/i18n.js
+++ b/lib/dojo/i18n.js
@@ -5,167 +5,254 @@
*/
-if(!dojo._hasResource["dojo.i18n"]){
-dojo._hasResource["dojo.i18n"]=true;
+if(!dojo._hasResource["dojo.i18n"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
+dojo._hasResource["dojo.i18n"] = true;
dojo.provide("dojo.i18n");
-dojo.i18n.getLocalization=function(_1,_2,_3){
-_3=dojo.i18n.normalizeLocale(_3);
-var _4=_3.split("-");
-var _5=[_1,"nls",_2].join(".");
-var _6=dojo._loadedModules[_5];
-if(_6){
-var _7;
-for(var i=_4.length;i>0;i--){
-var _8=_4.slice(0,i).join("_");
-if(_6[_8]){
-_7=_6[_8];
-break;
-}
-}
-if(!_7){
-_7=_6.ROOT;
-}
-if(_7){
-var _9=function(){
-};
-_9.prototype=_7;
-return new _9();
-}
-}
-throw new Error("Bundle not found: "+_2+" in "+_1+" , locale="+_3);
+
+/*=====
+dojo.i18n = {
+ // summary: Utility classes to enable loading of resources for internationalization (i18n)
};
-dojo.i18n.normalizeLocale=function(_a){
-var _b=_a?_a.toLowerCase():dojo.locale;
-if(_b=="root"){
-_b="ROOT";
-}
-return _b;
+=====*/
+
+dojo.i18n.getLocalization = function(/*String*/packageName, /*String*/bundleName, /*String?*/locale){
+ // summary:
+ // Returns an Object containing the localization for a given resource
+ // bundle in a package, matching the specified locale.
+ // description:
+ // Returns a hash containing name/value pairs in its prototypesuch
+ // that values can be easily overridden. Throws an exception if the
+ // bundle is not found. Bundle must have already been loaded by
+ // `dojo.requireLocalization()` or by a build optimization step. NOTE:
+ // try not to call this method as part of an object property
+ // definition (`var foo = { bar: dojo.i18n.getLocalization() }`). In
+ // some loading situations, the bundle may not be available in time
+ // for the object definition. Instead, call this method inside a
+ // function that is run after all modules load or the page loads (like
+ // in `dojo.addOnLoad()`), or in a widget lifecycle method.
+ // packageName:
+ // package which is associated with this resource
+ // bundleName:
+ // the base filename of the resource bundle (without the ".js" suffix)
+ // locale:
+ // the variant to load (optional). By default, the locale defined by
+ // the host environment: dojo.locale
+
+ locale = dojo.i18n.normalizeLocale(locale);
+
+ // look for nearest locale match
+ var elements = locale.split('-');
+ var module = [packageName,"nls",bundleName].join('.');
+ var bundle = dojo._loadedModules[module];
+ if(bundle){
+ var localization;
+ for(var i = elements.length; i > 0; i--){
+ var loc = elements.slice(0, i).join('_');
+ if(bundle[loc]){
+ localization = bundle[loc];
+ break;
+ }
+ }
+ if(!localization){
+ localization = bundle.ROOT;
+ }
+
+ // make a singleton prototype so that the caller won't accidentally change the values globally
+ if(localization){
+ var clazz = function(){};
+ clazz.prototype = localization;
+ return new clazz(); // Object
+ }
+ }
+
+ throw new Error("Bundle not found: " + bundleName + " in " + packageName+" , locale=" + locale);
};
-dojo.i18n._requireLocalization=function(_c,_d,_e,_f){
-var _10=dojo.i18n.normalizeLocale(_e);
-var _11=[_c,"nls",_d].join(".");
-var _12="";
-if(_f){
-var _13=_f.split(",");
-for(var i=0;i<_13.length;i++){
-if(_10["indexOf"](_13[i])==0){
-if(_13[i].length>_12.length){
-_12=_13[i];
-}
-}
-}
-if(!_12){
-_12="ROOT";
-}
-}
-var _14=_f?_12:_10;
-var _15=dojo._loadedModules[_11];
-var _16=null;
-if(_15){
-if(dojo.config.localizationComplete&&_15._built){
-return;
-}
-var _17=_14.replace(/-/g,"_");
-var _18=_11+"."+_17;
-_16=dojo._loadedModules[_18];
-}
-if(!_16){
-_15=dojo["provide"](_11);
-var _19=dojo._getModuleSymbols(_c);
-var _1a=_19.concat("nls").join("/");
-var _1b;
-dojo.i18n._searchLocalePath(_14,_f,function(loc){
-var _1c=loc.replace(/-/g,"_");
-var _1d=_11+"."+_1c;
-var _1e=false;
-if(!dojo._loadedModules[_1d]){
-dojo["provide"](_1d);
-var _1f=[_1a];
-if(loc!="ROOT"){
-_1f.push(loc);
-}
-_1f.push(_d);
-var _20=_1f.join("/")+".js";
-_1e=dojo._loadPath(_20,null,function(_21){
-var _22=function(){
+
+dojo.i18n.normalizeLocale = function(/*String?*/locale){
+ // summary:
+ // Returns canonical form of locale, as used by Dojo.
+ //
+ // description:
+ // All variants are case-insensitive and are separated by '-' as specified in [RFC 3066](http://www.ietf.org/rfc/rfc3066.txt).
+ // If no locale is specified, the dojo.locale is returned. dojo.locale is defined by
+ // the user agent's locale unless overridden by djConfig.
+
+ var result = locale ? locale.toLowerCase() : dojo.locale;
+ if(result == "root"){
+ result = "ROOT";
+ }
+ return result; // String
};
-_22.prototype=_1b;
-_15[_1c]=new _22();
-for(var j in _21){
-_15[_1c][j]=_21[j];
-}
-});
-}else{
-_1e=true;
-}
-if(_1e&&_15[_1c]){
-_1b=_15[_1c];
-}else{
-_15[_1c]=_1b;
-}
-if(_f){
-return true;
-}
-});
-}
-if(_f&&_10!=_12){
-_15[_10.replace(/-/g,"_")]=_15[_12.replace(/-/g,"_")];
-}
+
+dojo.i18n._requireLocalization = function(/*String*/moduleName, /*String*/bundleName, /*String?*/locale, /*String?*/availableFlatLocales){
+ // summary:
+ // See dojo.requireLocalization()
+ // description:
+ // Called by the bootstrap, but factored out so that it is only
+ // included in the build when needed.
+
+ var targetLocale = dojo.i18n.normalizeLocale(locale);
+ var bundlePackage = [moduleName, "nls", bundleName].join(".");
+ // NOTE:
+ // When loading these resources, the packaging does not match what is
+ // on disk. This is an implementation detail, as this is just a
+ // private data structure to hold the loaded resources. e.g.
+ // `tests/hello/nls/en-us/salutations.js` is loaded as the object
+ // `tests.hello.nls.salutations.en_us={...}` The structure on disk is
+ // intended to be most convenient for developers and translators, but
+ // in memory it is more logical and efficient to store in a different
+ // order. Locales cannot use dashes, since the resulting path will
+ // not evaluate as valid JS, so we translate them to underscores.
+
+ //Find the best-match locale to load if we have available flat locales.
+ var bestLocale = "";
+ if(availableFlatLocales){
+ var flatLocales = availableFlatLocales.split(",");
+ for(var i = 0; i < flatLocales.length; i++){
+ //Locale must match from start of string.
+ //Using ["indexOf"] so customBase builds do not see
+ //this as a dojo._base.array dependency.
+ if(targetLocale["indexOf"](flatLocales[i]) == 0){
+ if(flatLocales[i].length > bestLocale.length){
+ bestLocale = flatLocales[i];
+ }
+ }
+ }
+ if(!bestLocale){
+ bestLocale = "ROOT";
+ }
+ }
+
+ //See if the desired locale is already loaded.
+ var tempLocale = availableFlatLocales ? bestLocale : targetLocale;
+ var bundle = dojo._loadedModules[bundlePackage];
+ var localizedBundle = null;
+ if(bundle){
+ if(dojo.config.localizationComplete && bundle._built){return;}
+ var jsLoc = tempLocale.replace(/-/g, '_');
+ var translationPackage = bundlePackage+"."+jsLoc;
+ localizedBundle = dojo._loadedModules[translationPackage];
+ }
+
+ if(!localizedBundle){
+ bundle = dojo["provide"](bundlePackage);
+ var syms = dojo._getModuleSymbols(moduleName);
+ var modpath = syms.concat("nls").join("/");
+ var parent;
+
+ dojo.i18n._searchLocalePath(tempLocale, availableFlatLocales, function(loc){
+ var jsLoc = loc.replace(/-/g, '_');
+ var translationPackage = bundlePackage + "." + jsLoc;
+ var loaded = false;
+ if(!dojo._loadedModules[translationPackage]){
+ // Mark loaded whether it's found or not, so that further load attempts will not be made
+ dojo["provide"](translationPackage);
+ var module = [modpath];
+ if(loc != "ROOT"){module.push(loc);}
+ module.push(bundleName);
+ var filespec = module.join("/") + '.js';
+ loaded = dojo._loadPath(filespec, null, function(hash){
+ // Use singleton with prototype to point to parent bundle, then mix-in result from loadPath
+ var clazz = function(){};
+ clazz.prototype = parent;
+ bundle[jsLoc] = new clazz();
+ for(var j in hash){ bundle[jsLoc][j] = hash[j]; }
+ });
+ }else{
+ loaded = true;
+ }
+ if(loaded && bundle[jsLoc]){
+ parent = bundle[jsLoc];
+ }else{
+ bundle[jsLoc] = parent;
+ }
+
+ if(availableFlatLocales){
+ //Stop the locale path searching if we know the availableFlatLocales, since
+ //the first call to this function will load the only bundle that is needed.
+ return true;
+ }
+ });
+ }
+
+ //Save the best locale bundle as the target locale bundle when we know the
+ //the available bundles.
+ if(availableFlatLocales && targetLocale != bestLocale){
+ bundle[targetLocale.replace(/-/g, '_')] = bundle[bestLocale.replace(/-/g, '_')];
+ }
};
+
(function(){
-var _23=dojo.config.extraLocale;
-if(_23){
-if(!_23 instanceof Array){
-_23=[_23];
-}
-var req=dojo.i18n._requireLocalization;
-dojo.i18n._requireLocalization=function(m,b,_24,_25){
-req(m,b,_24,_25);
-if(_24){
-return;
-}
-for(var i=0;i<_23.length;i++){
-req(m,b,_23[i],_25);
-}
-};
-}
+ // If other locales are used, dojo.requireLocalization should load them as
+ // well, by default.
+ //
+ // Override dojo.requireLocalization to do load the default bundle, then
+ // iterate through the extraLocale list and load those translations as
+ // well, unless a particular locale was requested.
+
+ var extra = dojo.config.extraLocale;
+ if(extra){
+ if(!extra instanceof Array){
+ extra = [extra];
+ }
+
+ var req = dojo.i18n._requireLocalization;
+ dojo.i18n._requireLocalization = function(m, b, locale, availableFlatLocales){
+ req(m,b,locale, availableFlatLocales);
+ if(locale){return;}
+ for(var i=0; i<extra.length; i++){
+ req(m,b,extra[i], availableFlatLocales);
+ }
+ };
+ }
})();
-dojo.i18n._searchLocalePath=function(_26,_27,_28){
-_26=dojo.i18n.normalizeLocale(_26);
-var _29=_26.split("-");
-var _2a=[];
-for(var i=_29.length;i>0;i--){
-_2a.push(_29.slice(0,i).join("-"));
-}
-_2a.push(false);
-if(_27){
-_2a.reverse();
-}
-for(var j=_2a.length-1;j>=0;j--){
-var loc=_2a[j]||"ROOT";
-var _2b=_28(loc);
-if(_2b){
-break;
-}
-}
-};
-dojo.i18n._preloadLocalizations=function(_2c,_2d){
-function _2e(_2f){
-_2f=dojo.i18n.normalizeLocale(_2f);
-dojo.i18n._searchLocalePath(_2f,true,function(loc){
-for(var i=0;i<_2d.length;i++){
-if(_2d[i]==loc){
-dojo["require"](_2c+"_"+loc);
-return true;
-}
-}
-return false;
-});
+
+dojo.i18n._searchLocalePath = function(/*String*/locale, /*Boolean*/down, /*Function*/searchFunc){
+ // summary:
+ // A helper method to assist in searching for locale-based resources.
+ // Will iterate through the variants of a particular locale, either up
+ // or down, executing a callback function. For example, "en-us" and
+ // true will try "en-us" followed by "en" and finally "ROOT".
+
+ locale = dojo.i18n.normalizeLocale(locale);
+
+ var elements = locale.split('-');
+ var searchlist = [];
+ for(var i = elements.length; i > 0; i--){
+ searchlist.push(elements.slice(0, i).join('-'));
+ }
+ searchlist.push(false);
+ if(down){searchlist.reverse();}
+
+ for(var j = searchlist.length - 1; j >= 0; j--){
+ var loc = searchlist[j] || "ROOT";
+ var stop = searchFunc(loc);
+ if(stop){ break; }
+ }
};
-_2e();
-var _30=dojo.config.extraLocale||[];
-for(var i=0;i<_30.length;i++){
-_2e(_30[i]);
-}
+
+dojo.i18n._preloadLocalizations = function(/*String*/bundlePrefix, /*Array*/localesGenerated){
+ // summary:
+ // Load built, flattened resource bundles, if available for all
+ // locales used in the page. Only called by built layer files.
+
+ function preload(locale){
+ locale = dojo.i18n.normalizeLocale(locale);
+ dojo.i18n._searchLocalePath(locale, true, function(loc){
+ for(var i=0; i<localesGenerated.length;i++){
+ if(localesGenerated[i] == loc){
+ dojo["require"](bundlePrefix+"_"+loc);
+ return true; // Boolean
+ }
+ }
+ return false; // Boolean
+ });
+ }
+ preload();
+ var extra = dojo.config.extraLocale||[];
+ for(var i=0; i<extra.length; i++){
+ preload(extra[i]);
+ }
};
+
}