|
| 1 | +/* |
| 2 | + Copyright (c) 2004-2009, The Dojo Foundation All Rights Reserved. |
| 3 | + Available via the new BSD license. |
| 4 | + see: http://code.google.com/p/runjs/ for details |
| 5 | +*/ |
| 6 | +/*jslint regexp: false, nomen: false, plusplus: false */ |
| 7 | +/*global run: false, navigator: false */ |
| 8 | + |
| 9 | +"use strict"; |
| 10 | + |
| 11 | +(function () { |
| 12 | + //regexp for matching nls (i18n) module names. |
| 13 | + var nlsRegExp = /(^.*(^|\.)nls(\.|$))([^\.]*)\.?([^\.]*)/, |
| 14 | + empty = {}; |
| 15 | + |
| 16 | + /** |
| 17 | + * Does the work to integrate the bundle into the nls scheme. |
| 18 | + */ |
| 19 | + function integrateBundle(name, obj, context) { |
| 20 | + var i, bundle, parts, toLoad, nlsw, loc, val; |
| 21 | + |
| 22 | + //dep is an object, so it is an i18n nls thing. |
| 23 | + //Track it in the nls section of the context. |
| 24 | + //It may have already been created via a specific locale |
| 25 | + //request, so just mixin values in that case, to preserve |
| 26 | + //the specific locale bundle object. |
| 27 | + bundle = context.nls[name]; |
| 28 | + if (bundle) { |
| 29 | + run.mixin(bundle, obj); |
| 30 | + } else { |
| 31 | + context.nls[name] = obj; |
| 32 | + } |
| 33 | + |
| 34 | + //Break apart the locale to get the parts. |
| 35 | + parts = context.config.locale.split("-"); |
| 36 | + |
| 37 | + //Now see what bundles exist for each country/locale. |
| 38 | + //Want to walk up the chain, so if locale is en-us-foo, |
| 39 | + //look for en-us-foo, en-us, en, then root. |
| 40 | + toLoad = []; |
| 41 | + |
| 42 | + nlsw = context.nlsWaiting[name] || (context.nlsWaiting[name] = {}); |
| 43 | + for (i = parts.length; i > -1; i--) { |
| 44 | + loc = i ? parts.slice(0, i).join("-") : "root"; |
| 45 | + val = obj[loc]; |
| 46 | + if (val) { |
| 47 | + //Store which bundle to use for the default bundle definition. |
| 48 | + nlsw.__match = nlsw.__match || loc; |
| 49 | + |
| 50 | + //Track that the locale needs to be resolved with its parts. |
| 51 | + nlsw[loc] = true; |
| 52 | + |
| 53 | + //If locale value is a string, it means it is a resource that |
| 54 | + //needs to be loaded. Track it to load if it has not already |
| 55 | + //been asked for. |
| 56 | + if (typeof val === "string") { |
| 57 | + //Strip off the plugin prefix. |
| 58 | + val = val.substring(val.indexOf("!") + 1, val.length); |
| 59 | + |
| 60 | + if (!context.specified[val] && !(val in context.loaded)) { |
| 61 | + toLoad.push(val); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + //Load any bundles that are still needed. |
| 68 | + if (toLoad.length) { |
| 69 | + context.defined.run(toLoad); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + run.plugin({ |
| 74 | + prefix: "i18n", |
| 75 | + |
| 76 | + /** |
| 77 | + * This callback is prefix-specific, only gets called for this prefix |
| 78 | + */ |
| 79 | + run: function (name, deps, callback, context, isFunction) { |
| 80 | + var match, nlsw, bundle, master; |
| 81 | + |
| 82 | + integrateBundle(name, context.defined[name], context); |
| 83 | + |
| 84 | + //All i18n modules must match the nls module name structure. |
| 85 | + match = nlsRegExp.exec(name); |
| 86 | + //Reconstruct the master bundle name from parts of the regexp match |
| 87 | + //nlsRegExp.exec("foo.bar.baz.nls.en-ca.foo") gives: |
| 88 | + //["foo.bar.baz.nls.en-ca.foo", "foo.bar.baz.nls.", ".", ".", "en-ca", "foo"] |
| 89 | + //nlsRegExp.exec("foo.bar.baz.nls.foo") gives: |
| 90 | + //["foo.bar.baz.nls.foo", "foo.bar.baz.nls.", ".", ".", "foo", ""] |
| 91 | + //so, if match[5] is blank, it means this is the top bundle definition, |
| 92 | + //so it does not have to be handled. Only deal with ones that have a locale |
| 93 | + //(a match[4] value but no match[5]) |
| 94 | + if (match[5]) { |
| 95 | + master = match[1] + match[5]; |
| 96 | + |
| 97 | + //Track what locale bundle need to be generated once all the modules load. |
| 98 | + nlsw = (context.nlsWaiting[master] || (context.nlsWaiting[master] = {})); |
| 99 | + nlsw[match[4]] = true; |
| 100 | + |
| 101 | + bundle = context.nls[master]; |
| 102 | + if (!bundle) { |
| 103 | + //No master bundle yet, ask for it. |
| 104 | + context.defined.run([master]); |
| 105 | + bundle = context.nls[master] = {}; |
| 106 | + } |
| 107 | + //For nls modules, the callback is just a regular object, |
| 108 | + //so save it off in the bundle now. |
| 109 | + bundle[match[4]] = callback; |
| 110 | + } |
| 111 | + }, |
| 112 | + |
| 113 | + /** |
| 114 | + * Called when a new context is defined. Use this to store |
| 115 | + * context-specific info on it. |
| 116 | + */ |
| 117 | + newContext: function (context) { |
| 118 | + run.mixin(context, { |
| 119 | + nlsWaiting: {}, |
| 120 | + nls: {} |
| 121 | + }); |
| 122 | + if (!context.config.locale) { |
| 123 | + context.config.locale = typeof navigator === "undefined" ? "root" : |
| 124 | + (navigator.language || navigator.userLanguage || "root").toLowerCase(); |
| 125 | + } |
| 126 | + }, |
| 127 | + |
| 128 | + /** |
| 129 | + * Called when a dependency needs to be loaded. |
| 130 | + */ |
| 131 | + load: function (name, contextName) { |
| 132 | + //Peel off plugin prefix and call regular load. |
| 133 | + run.load(name.substring(name.indexOf("!") + 1, name.length), contextName); |
| 134 | + }, |
| 135 | + |
| 136 | + /** |
| 137 | + * Called when the dependencies of a module are checked. |
| 138 | + */ |
| 139 | + checkDeps: function (name, deps, context) { |
| 140 | + //If no dependencies, it means the bundle has already been |
| 141 | + //defined in the run call and skip it. |
| 142 | + if (!deps) { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + integrateBundle(name, deps, context); |
| 147 | + }, |
| 148 | + |
| 149 | + /** |
| 150 | + * Called to determine if a module is waiting to load. |
| 151 | + */ |
| 152 | + isWaiting: function (context) { |
| 153 | + return !!context.nlsWaiting.length; |
| 154 | + }, |
| 155 | + |
| 156 | + /** |
| 157 | + * Called when all modules have been loaded. |
| 158 | + */ |
| 159 | + orderDeps: function (context) { |
| 160 | + //Clear up state since further processing could |
| 161 | + //add more things to fetch. |
| 162 | + var i, prop, master, msWaiting, bundle, parts, moduleSuffix, mixed, |
| 163 | + modulePrefix, loc, defLoc, locPart, nlsWaiting = context.nlsWaiting; |
| 164 | + context.nlsWaiting = {}; |
| 165 | + |
| 166 | + //First, properly mix in any nls bundles waiting to happen. |
| 167 | + //Use an empty object to detect other bad JS code that modifies |
| 168 | + //Object.prototype. |
| 169 | + for (prop in nlsWaiting) { |
| 170 | + if (!(prop in empty)) { |
| 171 | + //Each property is a master bundle name. |
| 172 | + master = prop; |
| 173 | + msWaiting = nlsWaiting[prop]; |
| 174 | + bundle = context.nls[master]; |
| 175 | + defLoc = null; |
| 176 | + |
| 177 | + //Create the module name parts from the master name. So, if master |
| 178 | + //is foo.nls.bar, then the parts should be prefix: "foo.nls", |
| 179 | + // suffix: "bar", and the final locale's module name will be foo.nls.locale.bar |
| 180 | + parts = master.split("."); |
| 181 | + modulePrefix = parts.slice(0, parts.length - 1).join("."); |
| 182 | + moduleSuffix = parts[parts.length - 1]; |
| 183 | + //Cycle through the locale props on the waiting object and combine |
| 184 | + //the locales together. |
| 185 | + for (loc in msWaiting) { |
| 186 | + if (!(loc in empty)) { |
| 187 | + if (loc === "__match") { |
| 188 | + //Found default locale to use for the top-level bundle name. |
| 189 | + defLoc = msWaiting[loc]; |
| 190 | + } else { |
| 191 | + //Mix in the properties of this locale together. |
| 192 | + //Split the locale into pieces. |
| 193 | + mixed = {}; |
| 194 | + parts = loc.split("-"); |
| 195 | + for (i = parts.length; i > 0; i--) { |
| 196 | + locPart = parts.slice(0, i).join("-"); |
| 197 | + if (locPart !== "root" && bundle[locPart]) { |
| 198 | + run.mixin(mixed, bundle[locPart]); |
| 199 | + } |
| 200 | + } |
| 201 | + if (bundle.root) { |
| 202 | + run.mixin(mixed, bundle.root); |
| 203 | + } |
| 204 | + |
| 205 | + context.defined[modulePrefix + "." + loc + "." + moduleSuffix] = mixed; |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + //Finally define the default locale. Wait to the end of the property |
| 211 | + //loop above so that the default locale bundle has been properly mixed |
| 212 | + //together. |
| 213 | + context.defined[master] = context.defined[modulePrefix + "." + defLoc + "." + moduleSuffix]; |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + }); |
| 218 | +}()); |
0 commit comments