Changeset 7782

Show
Ignore:
Timestamp:
03/24/07 19:05:12 (22 months ago)
Author:
peller
Message:

dojo.i18n and tests. Refs #2624

Location:
dojo/trunk
Files:
2 modified
2 copied

Legend:

Unmodified
Added
Removed
  • dojo/trunk/i18n.js

    r7403 r7782  
    1 dojo.provide("dojo.i18n.common"); 
     1dojo.provide("dojo.i18n"); 
    22 
    33dojo.i18n.getLocalization = function(/*String*/packageName, /*String*/bundleName, /*String?*/locale){ 
     
    1616//              host environment: dojo.locale 
    1717 
    18         dojo.hostenv.preloadLocalizations(); 
    19         locale = dojo.hostenv.normalizeLocale(locale); 
     18        dojo.i18n._preloadLocalizations(); 
     19        locale = dojo.i18n.normalizeLocale(locale); 
    2020 
    2121        // look for nearest locale match 
    2222        var elements = locale.split('-'); 
    2323        var module = [packageName,"nls",bundleName].join('.'); 
    24         var bundle = dojo.hostenv.findModule(module, true); 
    25  
    26         var localization; 
     24        var bundle = dojo._loadedModules[module]; 
     25        if(bundle){ 
     26                var localization; 
     27                for(var i = elements.length; i > 0; i--){ 
     28                        var loc = elements.slice(0, i).join('_'); 
     29                        if(bundle[loc]){ 
     30                                localization = bundle[loc]; 
     31                                break; 
     32                        } 
     33                } 
     34                if(!localization){ 
     35                        localization = bundle.ROOT; 
     36                } 
     37 
     38                // make a singleton prototype so that the caller won't accidentally change the values globally 
     39                if(localization){ 
     40                        var clazz = function(){}; 
     41                        clazz.prototype = localization; 
     42                        return new clazz(); // Object 
     43                } 
     44        } 
     45 
     46        dojo.raise("Bundle not found: " + bundleName + " in " + packageName+" , locale=" + locale); 
     47}; 
     48 
     49dojo.i18n.normalizeLocale = function(/*String?*/locale){ 
     50        //      summary: 
     51        //              Returns canonical form of locale, as used by Dojo. 
     52        // 
     53        //  description: 
     54        //              All variants are case-insensitive and are separated by '-' as specified in RFC 3066. 
     55        //              If no locale is specified, the dojo.locale is returned.  dojo.locale is defined by 
     56        //              the user agent's locale unless overridden by djConfig. 
     57 
     58        var result = locale ? locale.toLowerCase() : dojo.locale; 
     59        if(result == "root"){ 
     60                result = "ROOT"; 
     61        } 
     62        return result; // String 
     63}; 
     64 
     65dojo.i18n._requireLocalization = function(/*String*/moduleName, /*String*/bundleName, /*String?*/locale, /*String?*/availableFlatLocales){ 
     66        // summary: 
     67        //      See dojo.requireLocalization() 
     68        // 
     69        // description: 
     70        //  Called by the bootstrap, but factored out so that it is only included in the build when needed. 
     71 
     72        dojo.i18n._preloadLocalizations(); 
     73        var targetLocale = dojo.i18n.normalizeLocale(locale); 
     74        var bundlePackage = [moduleName, "nls", bundleName].join("."); 
     75        // NOTE:  
     76        //              When loading these resources, the packaging does not match what is 
     77        //              on disk.  This is an implementation detail, as this is just a 
     78        //              private data structure to hold the loaded resources.  e.g. 
     79        //              tests/hello/nls/en-us/salutations.js is loaded as the object 
     80        //              tests.hello.nls.salutations.en_us={...} The structure on disk is 
     81        //              intended to be most convenient for developers and translators, but 
     82        //              in memory it is more logical and efficient to store in a different 
     83        //              order.  Locales cannot use dashes, since the resulting path will 
     84        //              not evaluate as valid JS, so we translate them to underscores. 
     85         
     86        //Find the best-match locale to load if we have available flat locales. 
     87        var bestLocale = ""; 
     88        if(availableFlatLocales){ 
     89                var flatLocales = availableFlatLocales.split(","); 
     90                for(var i = 0; i < flatLocales.length; i++){ 
     91                        //Locale must match from start of string. 
     92                        if(targetLocale.indexOf(flatLocales[i]) == 0){ 
     93                                if(flatLocales[i].length > bestLocale.length){ 
     94                                        bestLocale = flatLocales[i]; 
     95                                } 
     96                        } 
     97                } 
     98                if(!bestLocale){ 
     99                        bestLocale = "ROOT"; 
     100                }                
     101        } 
     102 
     103        //See if the desired locale is already loaded. 
     104        var tempLocale = availableFlatLocales ? bestLocale : targetLocale; 
     105        var bundle = dojo._loadedModules[bundlePackage]; 
     106        var localizedBundle = null; 
     107        if(bundle){ 
     108                if(djConfig.localizationComplete && bundle._built){return;} 
     109                var jsLoc = tempLocale.replace('-', '_', 'g'); 
     110                var translationPackage = bundlePackage+"."+jsLoc; 
     111                localizedBundle = dojo._loadedModules[translationPackage]; 
     112        } 
     113 
     114        if(!localizedBundle){ 
     115                bundle = dojo["provide"](bundlePackage); 
     116                var syms = dojo._getModuleSymbols(moduleName); 
     117                var modpath = syms.concat("nls").join("/"); 
     118                var parent; 
     119 
     120                dojo.i18n._searchLocalePath(tempLocale, availableFlatLocales, function(loc){ 
     121                        var jsLoc = loc.replace('-', '_', 'g'); 
     122                        var translationPackage = bundlePackage + "." + jsLoc; 
     123                        var loaded = false; 
     124                        if(!dojo._loadedModules[translationPackage]){ 
     125                                // Mark loaded whether it's found or not, so that further load attempts will not be made 
     126                                dojo["provide"](translationPackage); 
     127                                var module = [modpath]; 
     128                                if(loc != "ROOT"){module.push(loc);} 
     129                                module.push(bundleName); 
     130                                var filespec = module.join("/") + '.js'; 
     131                                loaded = dojo._loadPath(filespec, null, function(hash){ 
     132                                        // Use singleton with prototype to point to parent bundle, then mix-in result from loadPath 
     133                                        var clazz = function(){}; 
     134                                        clazz.prototype = parent; 
     135                                        bundle[jsLoc] = new clazz(); 
     136                                        for(var j in hash){ bundle[jsLoc][j] = hash[j]; } 
     137                                }); 
     138                        }else{ 
     139                                loaded = true; 
     140                        } 
     141                        if(loaded && bundle[jsLoc]){ 
     142                                parent = bundle[jsLoc]; 
     143                        }else{ 
     144                                bundle[jsLoc] = parent; 
     145                        } 
     146                         
     147                        if(availableFlatLocales){ 
     148                                //Stop the locale path searching if we know the availableFlatLocales, since 
     149                                //the first call to this function will load the only bundle that is needed. 
     150                                return true; 
     151                        } 
     152                }); 
     153        } 
     154 
     155        //Save the best locale bundle as the target locale bundle when we know the 
     156        //the available bundles. 
     157        if(availableFlatLocales && targetLocale != bestLocale){ 
     158                bundle[targetLocale.replace('-', '_', 'g')] = bundle[bestLocale.replace('-', '_', 'g')]; 
     159        } 
     160}; 
     161 
     162(function(){ 
     163        // If other locales are used, dojo.requireLocalization should load them as 
     164        // well, by default.  
     165        //  
     166        // Override dojo.requireLocalization to do load the default bundle, then 
     167        // iterate through the extraLocale list and load those translations as 
     168        // well, unless a particular locale was requested. 
     169 
     170        var extra = djConfig.extraLocale; 
     171        if(extra){ 
     172                if(!extra instanceof Array){ 
     173                        extra = [extra]; 
     174                } 
     175 
     176                var req = dojo.i18n._requireLocalization; 
     177                dojo.i18n._requireLocalization = function(m, b, locale, availableFlatLocales){ 
     178                        req(m,b,locale, availableFlatLocales); 
     179                        if(locale){return;} 
     180                        for(var i=0; i<extra.length; i++){ 
     181                                req(m,b,extra[i], availableFlatLocales); 
     182                        } 
     183                }; 
     184        } 
     185})(); 
     186 
     187dojo.i18n._searchLocalePath = function(/*String*/locale, /*Boolean*/down, /*Function*/searchFunc){ 
     188        //      summary: 
     189        //              A helper method to assist in searching for locale-based resources. 
     190        //              Will iterate through the variants of a particular locale, either up 
     191        //              or down, executing a callback function.  For example, "en-us" and 
     192        //              true will try "en-us" followed by "en" and finally "ROOT". 
     193 
     194        locale = dojo.i18n.normalizeLocale(locale); 
     195 
     196        var elements = locale.split('-'); 
     197        var searchlist = []; 
    27198        for(var i = elements.length; i > 0; i--){ 
    28                 var loc = elements.slice(0, i).join('_'); 
    29                 if(bundle[loc]){ 
    30                         localization = bundle[loc]; 
    31                         break; 
    32                 } 
    33         } 
    34         if(!localization){ 
    35                 localization = bundle.ROOT; 
    36         } 
    37  
    38         // make a singleton prototype so that the caller won't accidentally change the values globally 
    39         if(localization){ 
    40                 var clazz = function(){}; 
    41                 clazz.prototype = localization; 
    42                 return new clazz(); // Object 
    43         } 
    44  
    45         dojo.raise("Bundle not found: " + bundleName + " in " + packageName+" , locale=" + locale); 
    46 }; 
    47  
    48 dojo.i18n.isLeftToRight = function(/*String?*/locale){ 
    49 //      summary: 
    50 //              Is the language read left-to-right for the given locale?  Most exceptions are for middle eastern languages. 
    51 // 
    52 //      locale: a string representing the locale.  By default, the locale defined by the 
    53 //              host environment: dojo.locale 
    54  
    55         var lang = dojo.hostenv.normalizeLocale(locale).split('-')[0]; 
    56         var RTL = {ar:true,fa:true,he:true,ur:true,yi:true}; 
    57         return !RTL[lang]; // Boolean 
    58 }; 
     199                searchlist.push(elements.slice(0, i).join('-')); 
     200        } 
     201        searchlist.push(false); 
     202        if(down){searchlist.reverse();} 
     203 
     204        for(var j = searchlist.length - 1; j >= 0; j--){ 
     205                var loc = searchlist[j] || "ROOT"; 
     206                var stop = searchFunc(loc); 
     207                if(stop){ break; } 
     208        } 
     209}; 
     210 
     211//These two functions are placed outside of preloadLocalizations 
     212//So that the xd loading can use/override them. 
     213dojo.i18n._localesGenerated /***BUILD:localesGenerated***/; // value will be inserted here at build time, if necessary 
     214 
     215dojo.i18n._preloadLocalizations = function(){ 
     216        // summary: 
     217        //              Load built, flattened resource bundles, if available for all 
     218        //              locales used in the page. Execute only once. Note that this is a 
     219        //              no-op unless there is a build. 
     220 
     221        if(dojo.i18n._localesGenerated){ 
     222                dojo.registerModulePath("nls","nls");    
     223 
     224                function preload(locale){ 
     225                        locale = dojo.i18n.normalizeLocale(locale); 
     226                        dojo.i18n._searchLocalePath(locale, true, function(loc){ 
     227                                for(var i=0; i<dojo.i18n._localesGenerated.length;i++){ 
     228                                        if(dojo.i18n._localesGenerated[i] == loc){ 
     229                                                dojo["require"]("nls.dojo_"+loc); 
     230                                                return true; // Boolean 
     231                                        } 
     232                                } 
     233                                return false; // Boolean 
     234                        }); 
     235                } 
     236                preload(); 
     237                var extra = djConfig.extraLocale||[]; 
     238                for(var i=0; i<extra.length; i++){ 
     239                        preload(extra[i]); 
     240                } 
     241        } 
     242        dojo.i18n._preloadLocalizations = function(){}; 
     243}; 
  • dojo/trunk/tests/_base.js

    r7750 r7782  
    121121        dojo.require("tests._base.json"); 
    122122        // FIXME: add test includes for the rest of the Dojo Base groups here 
     123        dojo.require("tests.i18n"); //FIXME does not belong here in _base 
    123124}catch(e){ 
    124125        tests.debug(e); 
  • dojo/trunk/_base/_loader/hostenv_browser.js

    r7774 r7782  
    5353        })(); 
    5454 
     55        dojo.locale = dojo.locale || 
     56          (dojo.isIE ? navigator.userLanguage : navigator.language).toLowerCase(); 
     57 
    5558        dojo._println = console.debug; 
    5659 
     
    310313                                // if more than 1, choose first with the correct id 
    311314                                var i=0, te; 
    312                                 while(te=eles[i++]){ 
     315                                while((te=eles[i++])){ 
    313316                                        if(te.id == id){ return te; } 
    314317                                }