source: Dev/branches/rest-dojo-ui/client/dojo/i18n.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 7.1 KB
RevLine 
[256]1define(["./_base/kernel", "require", "./has", "./_base/array", "./_base/lang", "./_base/xhr"], function(dojo, require, has, array, lang) {
2        // module:
3        //              dojo/i18n
4        // summary:
5        //              This module implements the !dojo/i18n plugin and the v1.6- i18n API
6        // description:
7        //              We choose to include our own plugin to leverage functionality already contained in dojo
8        //              and thereby reduce the size of the plugin compared to various loader implementations. Also, this
9        //              allows foreign AMD loaders to be used without their plugins.
10        var
11                thisModule= dojo.i18n=
12                        // the dojo.i18n module
13                        {},
14
15                nlsRe=
16                        // regexp for reconstructing the master bundle name from parts of the regexp match
17                        // nlsRe.exec("foo/bar/baz/nls/en-ca/foo") gives:
18                        // ["foo/bar/baz/nls/en-ca/foo", "foo/bar/baz/nls/", "/", "/", "en-ca", "foo"]
19                        // nlsRe.exec("foo/bar/baz/nls/foo") gives:
20                        // ["foo/bar/baz/nls/foo", "foo/bar/baz/nls/", "/", "/", "foo", ""]
21                        // so, if match[5] is blank, it means this is the top bundle definition.
22                        // courtesy of http://requirejs.org
23                        /(^.*(^|\/)nls)(\/|$)([^\/]*)\/?([^\/]*)/,
24
25                getAvailableLocales= function(
26                        root,
27                        locale,
28                        bundlePath,
29                        bundleName
30                ){
31                        // return a vector of module ids containing all available locales with respect to the target locale
32                        // For example, assuming:
33                        //       * the root bundle indicates specific bundles for "fr" and "fr-ca",
34                        //       * bundlePath is "myPackage/nls"
35                        //       * bundleName is "myBundle"
36                        // Then a locale argument of "fr-ca" would return
37                        //       ["myPackage/nls/myBundle", "myPackage/nls/fr/myBundle", "myPackage/nls/fr-ca/myBundle"]
38                        // Notice that bundles are returned least-specific to most-specific, starting with the root.
39                        //
40                        // If root===false indicates we're working with a pre-AMD i18n bundle that doesn't tell about the available locales;
41                        // therefore, assume everything is available and get 404 errors that indicate a particular localization is not available
42                        //
43
44                        for(var result= [bundlePath + bundleName], localeParts= locale.split("-"), current= "", i= 0; i<localeParts.length; i++){
45                                current+= (current ? "-" : "") + localeParts[i];
46                                if(!root || root[current]){
47                                        result.push(bundlePath + current + "/" + bundleName);
48                                }
49                        }
50                        return result;
51                },
52
53                cache= {},
54
55                getL10nName= dojo.getL10nName = function(moduleName, bundleName, locale){
56                        locale = locale ? locale.toLowerCase() : dojo.locale;
57                        moduleName = "dojo/i18n!" + moduleName.replace(/\./g, "/");
58                        bundleName = bundleName.replace(/\./g, "/");
59                        return (/root/i.test(locale)) ?
60                                (moduleName + "/nls/" + bundleName) :
61                                (moduleName + "/nls/" + locale + "/" + bundleName);
62                },
63
64                doLoad = function(require, bundlePathAndName, bundlePath, bundleName, locale, load){
65                        // get the root bundle which instructs which other bundles are required to contruct the localized bundle
66                        require([bundlePathAndName], function(root){
67                                var
68                                        current= cache[bundlePathAndName + "/"]= lang.clone(root.root),
69                                        availableLocales= getAvailableLocales(!root._v1x && root, locale, bundlePath, bundleName);
70                                require(availableLocales, function(){
71                                        for (var i= 1; i<availableLocales.length; i++){
72                                                cache[availableLocales[i]]= current= lang.mixin(lang.clone(current), arguments[i]);
73                                        }
74                                        // target may not have been resolve (e.g., maybe only "fr" exists when "fr-ca" was requested)
75                                        var target= bundlePathAndName + "/" + locale;
76                                        cache[target]= current;
77                                        load && load(lang.delegate(current));
78                                });
79                        });
80                },
81
82                normalize = function(id, toAbsMid){
83                        // note: id may be relative
84                        var match= nlsRe.exec(id),
85                                bundlePath= match[1];
86                        return /^\./.test(bundlePath) ? toAbsMid(bundlePath) + "/" +  id.substring(bundlePath.length) : id;
87                };
88
89                load = function(id, require, load){
90                        // note: id is always absolute
91                        var
92                                match= nlsRe.exec(id),
93                                bundlePath= match[1] + "/",
94                                bundleName= match[5] || match[4],
95                                bundlePathAndName= bundlePath + bundleName,
96                                localeSpecified = (match[5] && match[4]),
97                                targetLocale=  localeSpecified || dojo.locale,
98                                target= bundlePathAndName + "/" + targetLocale;
99
100                        if(localeSpecified){
101                                if(cache[target]){
102                                        // a request for a specific local that has already been loaded; just return it
103                                        load(cache[target]);
104                                }else{
105                                        // a request for a specific local that has not been loaded; load and return just that locale
106                                        doLoad(require, bundlePathAndName, bundlePath, bundleName, targetLocale, load);
107                                }
108                                return;
109                        }// else a non-locale-specific request; therefore always load dojo.locale + dojo.config.extraLocale
110
111                        // notice the subtle algorithm that loads targeLocal last, which is the only doLoad application that passes a value for the load callback
112                        // this makes the sync loader follow a clean code path that loads extras first and then proceeds with tracing the current deps graph
113                        var extra = dojo.config.extraLocale || [];
114                        extra = lang.isArray(extra) ? extra : [extra];
115                        extra.push(targetLocale);
116                        array.forEach(extra, function(locale){
117                                doLoad(require, bundlePathAndName, bundlePath, bundleName, locale, locale==targetLocale && load);
118                        });
119                };
120
121
122        has.add("dojo-v1x-i18n-Api",
123                // if true, define the v1.x i18n functions
124                1
125        );
126
127        if(has("dojo-v1x-i18n-Api")){
128                var
129                        evalBundle=
130                                // keep the minifiers off our define!
131                                // if bundle is an AMD bundle, then __amdResult will be defined; otherwise it's a pre-amd bundle and the bundle value is returned by eval
132                                new Function("bundle", "var __preAmdResult, __amdResult; function define(bundle){__amdResult= bundle;} __preAmdResult= eval(bundle); return [__preAmdResult, __amdResult];"),
133
134                        fixup= function(url, preAmdResult, amdResult){
135                                // nls/<locale>/<bundle-name> indicates not the root.
136                                return preAmdResult ? (/nls\/[^\/]+\/[^\/]+$/.test(url) ? preAmdResult : {root:preAmdResult, _v1x:1}) : amdResult;
137                        },
138
139                        syncRequire= function(deps, callback){
140                                var results= [];
141                                dojo.forEach(deps, function(mid){
142                                        var url= require.toUrl(mid + ".js");
143                                        if(cache[url]){
144                                                results.push(cache[url]);
145                                        }else{
146
147                                                try {
148                                                        var bundle= require(mid);
149                                                        if(bundle){
150                                                                results.push(bundle);
151                                                                return;
152                                                        }
153                                                }catch(e){}
154
155                                                dojo.xhrGet({
156                                                        url:url,
157                                                        sync:true,
158                                                        load:function(text){
159                                                                var result = evalBundle(text);
160                                                                results.push(cache[url]= fixup(url, result[0], result[1]));
161                                                        },
162                                                        error:function(){
163                                                                results.push(cache[url]= {});
164                                                        }
165                                                });
166                                        }
167                                });
168                                callback.apply(null, results);
169                        };
170
171                thisModule.getLocalization= function(moduleName, bundleName, locale){
172                        var result,
173                                l10nName= getL10nName(moduleName, bundleName, locale).substring(10);
174                        load(l10nName, (has("dojo-sync-loader") && !require.isXdUrl(require.toUrl(l10nName + ".js")) ? syncRequire : require), function(result_){ result= result_; });
175                        return result;
176                };
177
178                thisModule.normalizeLocale= function(locale){
179                        var result = locale ? locale.toLowerCase() : dojo.locale;
180                        if(result == "root"){
181                                result = "ROOT";
182                        }
183                        return result;
184                };
185        }
186
187        return lang.mixin(thisModule, {
188                dynamic:true,
189                normalize:normalize,
190                load:load,
191                cache:function(mid, value){
192                        cache[mid] = value;
193                }
194        });
195});
Note: See TracBrowser for help on using the repository browser.