Changeset 7291 for branches/dojo-lite

Show
Ignore:
Timestamp:
02/12/07 21:51:22 (22 months ago)
Author:
peller
Message:

get i18n loading code out of the bootstrap

Location:
branches/dojo-lite
Files:
1 added
3 modified

Legend:

Unmodified
Added
Removed
  • branches/dojo-lite/buildscripts/buildUtil.js

    r7008 r7291  
    248248                } 
    249249        } 
     250 
     251        // dojo.requireLocalization is a special case as it pulls in dojo.i18n.loader at runtime 
     252        if(dojoContents.match(buildUtil.globalRequireLocalizationRegExp)){ 
     253                depList.push("../src/i18n/loader.js"); 
     254                dojoContents += new String(readFile(depList[depList.length-1])); 
     255        } 
     256 
    250257         
    251258        //Move all the dojo.provide calls to the top, and remove any matching dojo.require calls. 
     
    439446         
    440447        //Get the generated, flattened bundle. 
    441         var module = dojo.evalObjPath(moduleName); 
     448        var module = dojo.getObject(moduleName); 
    442449        var bundleLocale = localeName ? localeName.replace(/-/g, "_") : "ROOT"; 
    443450        var flattenedBundle = module.nls[bundleName][bundleLocale]; 
  • branches/dojo-lite/src/loader.js

    r6991 r7291  
    529529        //              dojo.registerModulePath.  
    530530        return dojo.hostenv.registerModulePath(module, prefix); 
    531 } 
    532  
    533 // Localization routines 
    534  
    535 dojo.hostenv.normalizeLocale = function(/*String?*/locale){ 
    536         //      summary: 
    537         //              Returns canonical form of locale, as used by Dojo.  All variants 
    538         //              are case-insensitive and are separated by '-' as specified in RFC 
    539         //              3066. If no locale is specified, the user agent's default is 
    540         //              returned. 
    541  
    542         var result = locale ? locale.toLowerCase() : dojo.locale; 
    543         if(result == "root"){ 
    544                 result = "ROOT"; 
    545         } 
    546         return result;// String 
    547 }; 
    548  
    549 dojo.hostenv.searchLocalePath = function(/*String*/locale, /*Boolean*/down, /*Function*/searchFunc){ 
    550         //      summary: 
    551         //              A helper method to assist in searching for locale-based resources. 
    552         //              Will iterate through the variants of a particular locale, either up 
    553         //              or down, executing a callback function.  For example, "en-us" and 
    554         //              true will try "en-us" followed by "en" and finally "ROOT". 
    555  
    556         locale = dojo.hostenv.normalizeLocale(locale); 
    557  
    558         var elements = locale.split('-'); 
    559         var searchlist = []; 
    560         for(var i = elements.length; i > 0; i--){ 
    561                 searchlist.push(elements.slice(0, i).join('-')); 
    562         } 
    563         searchlist.push(false); 
    564         if(down){searchlist.reverse();} 
    565  
    566         for(var j = searchlist.length - 1; j >= 0; j--){ 
    567                 var loc = searchlist[j] || "ROOT"; 
    568                 var stop = searchFunc(loc); 
    569                 if(stop){ break; } 
    570         } 
    571 } 
    572  
    573 //These two functions are placed outside of preloadLocalizations 
    574 //So that the xd loading can use/override them. 
    575 dojo.hostenv.localesGenerated /***BUILD:localesGenerated***/; // value will be inserted here at build time, if necessary 
    576  
    577 dojo.hostenv.registerNlsPrefix = function(){ 
    578         // summary: 
    579         //              Register module "nls" to point where Dojo can find pre-built 
    580         //              localization files 
    581         dojo.registerModulePath("nls","nls");    
    582 } 
    583  
    584 dojo.hostenv.preloadLocalizations = function(){ 
    585         // summary: 
    586         //              Load built, flattened resource bundles, if available for all 
    587         //              locales used in the page. Execute only once. Note that this is a 
    588         //              no-op unless there is a build. 
    589  
    590         if(dojo.hostenv.localesGenerated){ 
    591                 dojo.hostenv.registerNlsPrefix(); 
    592  
    593                 function preload(locale){ 
    594                         locale = dojo.hostenv.normalizeLocale(locale); 
    595                         dojo.hostenv.searchLocalePath(locale, true, function(loc){ 
    596                                 for(var i=0; i<dojo.hostenv.localesGenerated.length;i++){ 
    597                                         if(dojo.hostenv.localesGenerated[i] == loc){ 
    598                                                 dojo["require"]("nls.dojo_"+loc); 
    599                                                 return true; // Boolean 
    600                                         } 
    601                                 } 
    602                                 return false; // Boolean 
    603                         }); 
    604                 } 
    605                 preload(); 
    606                 var extra = djConfig.extraLocale||[]; 
    607                 for(var i=0; i<extra.length; i++){ 
    608                         preload(extra[i]); 
    609                 } 
    610         } 
    611         dojo.hostenv.preloadLocalizations = function(){}; 
    612531} 
    613532 
     
    674593        //              required to load these resources. 
    675594 
    676         dojo.hostenv.preloadLocalizations(); 
    677         var targetLocale = dojo.hostenv.normalizeLocale(locale); 
    678         var bundlePackage = [moduleName, "nls", bundleName].join("."); 
    679         // NOTE:  
    680         //              When loading these resources, the packaging does not match what is 
    681         //              on disk.  This is an implementation detail, as this is just a 
    682         //              private data structure to hold the loaded resources.  e.g. 
    683         //              tests/hello/nls/en-us/salutations.js is loaded as the object 
    684         //              tests.hello.nls.salutations.en_us={...} The structure on disk is 
    685         //              intended to be most convenient for developers and translators, but 
    686         //              in memory it is more logical and efficient to store in a different 
    687         //              order.  Locales cannot use dashes, since the resulting path will 
    688         //              not evaluate as valid JS, so we translate them to underscores. 
    689          
    690         //Find the best-match locale to load if we have available flat locales. 
    691         var bestLocale = ""; 
    692         if(availableFlatLocales){ 
    693                 var flatLocales = availableFlatLocales.split(","); 
    694                 for(var i = 0; i < flatLocales.length; i++){ 
    695                         //Locale must match from start of string. 
    696                         if(targetLocale.indexOf(flatLocales[i]) == 0){ 
    697                                 if(flatLocales[i].length > bestLocale.length){ 
    698                                         bestLocale = flatLocales[i]; 
    699                                 } 
    700                         } 
    701                 } 
    702                 if(!bestLocale){ 
    703                         bestLocale = "ROOT"; 
    704                 }                
    705         } 
    706  
    707         //See if the desired locale is already loaded. 
    708         var tempLocale = availableFlatLocales ? bestLocale : targetLocale; 
    709         var bundle = dojo.hostenv.findModule(bundlePackage); 
    710         var localizedBundle = null; 
    711         if(bundle){ 
    712                 if(djConfig.localizationComplete && bundle._built){return;} 
    713                 var jsLoc = tempLocale.replace('-', '_'); 
    714                 var translationPackage = bundlePackage+"."+jsLoc; 
    715                 localizedBundle = dojo.hostenv.findModule(translationPackage); 
    716         } 
    717  
    718         if(!localizedBundle){ 
    719                 bundle = dojo.hostenv.startPackage(bundlePackage); 
    720                 var syms = dojo.hostenv.getModuleSymbols(moduleName); 
    721                 var modpath = syms.concat("nls").join("/"); 
    722                 var parent; 
    723  
    724                 dojo.hostenv.searchLocalePath(tempLocale, availableFlatLocales, function(loc){ 
    725                         var jsLoc = loc.replace('-', '_'); 
    726                         var translationPackage = bundlePackage + "." + jsLoc; 
    727                         var loaded = false; 
    728                         if(!dojo.hostenv.findModule(translationPackage)){ 
    729                                 // Mark loaded whether it's found or not, so that further load attempts will not be made 
    730                                 dojo.hostenv.startPackage(translationPackage); 
    731                                 var module = [modpath]; 
    732                                 if(loc != "ROOT"){module.push(loc);} 
    733                                 module.push(bundleName); 
    734                                 var filespec = module.join("/") + '.js'; 
    735                                 loaded = dojo.hostenv.loadPath(filespec, null, function(hash){ 
    736                                         // Use singleton with prototype to point to parent bundle, then mix-in result from loadPath 
    737                                         var clazz = function(){}; 
    738                                         clazz.prototype = parent; 
    739                                         bundle[jsLoc] = new clazz(); 
    740                                         for(var j in hash){ bundle[jsLoc][j] = hash[j]; } 
    741                                 }); 
    742                         }else{ 
    743                                 loaded = true; 
    744                         } 
    745                         if(loaded && bundle[jsLoc]){ 
    746                                 parent = bundle[jsLoc]; 
    747                         }else{ 
    748                                 bundle[jsLoc] = parent; 
    749                         } 
    750                          
    751                         if(availableFlatLocales){ 
    752                                 //Stop the locale path searching if we know the availableFlatLocales, since 
    753                                 //the first call to this function will load the only bundle that is needed. 
    754                                 return true; 
    755                         } 
    756                 }); 
    757         } 
    758  
    759         //Save the best locale bundle as the target locale bundle when we know the 
    760         //the available bundles. 
    761         if(availableFlatLocales && targetLocale != bestLocale){ 
    762                 bundle[targetLocale.replace('-', '_')] = bundle[bestLocale.replace('-', '_')]; 
    763         } 
    764 }; 
    765  
    766 (function(){ 
    767         // If other locales are used, dojo.requireLocalization should load them as 
    768         // well, by default.  
    769         //  
    770         // Override dojo.requireLocalization to do load the default bundle, then 
    771         // iterate through the extraLocale list and load those translations as 
    772         // well, unless a particular locale was requested. 
    773  
    774         var extra = djConfig.extraLocale; 
    775         if(extra){ 
    776                 if(!extra instanceof Array){ 
    777                         extra = [extra]; 
    778                 } 
    779  
    780                 var req = dojo.requireLocalization; 
    781                 dojo.requireLocalization = function(m, b, locale, availableFlatLocales){ 
    782                         req(m,b,locale, availableFlatLocales); 
    783                         if(locale){return;} 
    784                         for(var i=0; i<extra.length; i++){ 
    785                                 req(m,b,extra[i], availableFlatLocales); 
    786                         } 
    787                 }; 
    788         } 
    789 })(); 
     595        dojo.require("dojo.i18n.loader"); 
     596        dojo.i18n._requireLocalization.apply(dojo.hostenv, arguments); 
     597} 
     598 
     599 
    790600// vim:ai:ts=4:noet:textwidth=80 
  • branches/dojo-lite/src/loader_stub.js

    r7013 r7291  
    6464        var hostenv_phantoms = [ 
    6565                "loadPath", "loadUri", "loadUriAndCheck", "unloaded", "loadModule", 
    66                 "preloadLocalizations", "registerNlsPrefix", "preloadLocalizations", 
    67                 "searchLocalePath", "normalizeLocale", "findModule", "getModuleSymbols" 
     66                "preloadLocalizations", "normalizeLocale", "findModule", "getModuleSymbols" 
    6867        ]; 
    6968        for(var x = 0; x < hostenv_phantoms.length; x++){ 
     
    246245} 
    247246 
    248 //These two functions are placed outside of preloadLocalizations 
    249 //So that the xd loading can use/override them. 
    250 dojo.hostenv.localesGenerated /***BUILD:localesGenerated***/; // value will be inserted here at build time, if necessary 
    251  
    252 dojo.requireLocalization = function(){ return false; }; 
     247dojo.requireLocalization = function(/*...*/){ 
     248        //summary: see dojo.i18n.requireLocalization 
     249        dojo.require("dojo.i18n.loader"); 
     250        dojo.i18n._requireLocalization.apply(dojo.hostenv, arguments); 
     251}