better handling of shared/global language bundles #590

Merged
merged 5 commits into from Oct 4, 2012
@@ -65,9 +65,9 @@ YUI.add('PagedFlickr', function(Y, NAME) {
}, '0.0.1', {requires: [
'mojito-intl-addon',
- 'mojito-models-addon'
- 'mojito-params-addon'
- 'mojito-url-addon'
+ 'mojito-models-addon',
+ 'mojito-params-addon',
+ 'mojito-url-addon',
'mojito-util',
'PagedFlickrModel'
], lang: ['de', 'en-US']});
@@ -177,7 +177,8 @@ YUI.add('addon-rs-yui', function(Y, NAME) {
parseResourceVersion: function(source, type, subtype, mojitType) {
var fs = source.fs,
baseParts,
- res;
+ res,
+ sandbox;
if ('yui-lang' === type) {
res = {
@@ -190,14 +191,16 @@ YUI.add('addon-rs-yui', function(Y, NAME) {
if (!res.yui) {
res.yui = {};
}
- if (fs.basename === mojitType) {
- res.yui.lang = '';
- } else if (mojitType === fs.basename.substr(0, mojitType.length)) {
- res.yui.lang = fs.basename.substr(mojitType.length + 1);
- } else {
- Y.log('invalid YUI lang file format. skipping ' + fs.fullPath, 'error', NAME);
- }
- res.name = res.yui.lang;
+ sandbox = {
+ Intl: {
+ add: function(langFor, lang) {
+ res.yui.langFor = langFor;
+ res.yui.lang = lang;
+ }
+ }
+ };
+ this._captureYUIModuleDetails(res, sandbox);
+ res.name = res.yui.name;
res.id = [res.type, res.subtype, res.name].join('-');
return new Y.Do.Halt(null, res);
}
@@ -332,12 +335,10 @@ YUI.add('addon-rs-yui', function(Y, NAME) {
ress = evt.ress,
r,
res,
- langs = {},
+ langRess = {}, // YUI module name: language: resource
l,
- ll,
- lang,
langName,
- langNames,
+ langNames = {},
langSorted,
viewEngineRequired = {},
modules = {},
@@ -369,7 +370,11 @@ YUI.add('addon-rs-yui', function(Y, NAME) {
viewEngineRequired[res.yui.name] = true;
}
if ('yui-lang' === res.type) {
- langs[res.name] = res;
+ langNames[res.yui.lang] = true;
+ if (!langRess[res.yui.langFor]) {
+ langRess[res.yui.langFor] = {};
+ }
+ langRess[res.yui.langFor][res.yui.lang] = res;
}
if (res.yui && res.yui.name) {
modules[res.yui.name] = this._makeYUIModuleConfig(env, res);
@@ -394,10 +399,8 @@ YUI.add('addon-rs-yui', function(Y, NAME) {
this.modules[env][poslKey][mojit] = Y.mojito.util.copy(modules);
// we always want to do calculations for no-lang
- if (!langs['']) {
- langs[''] = undefined;
- }
- langNames = Object.keys(langs);
+ langNames[''] = true;
+ langNames = Object.keys(langNames);
if (controller) {
required = { 'mojito-dispatcher': true };
@@ -409,12 +412,10 @@ YUI.add('addon-rs-yui', function(Y, NAME) {
for (l = 0; l < langNames.length; l += 1) {
langName = langNames[l];
- lang = langs[langName];
langSorted = Y.mojito.util.copy(sorted);
- this._addLangToSorted(env, langSorted, lang);
+ this._addLangsToSorted(env, langSorted, langName, langRess);
this._setYUIDependencies(env, poslKey, langName, controller.yui.name, langSorted);
}
-
}
if ('client' === env) {
for (binderName in binders) {
@@ -428,9 +429,8 @@ YUI.add('addon-rs-yui', function(Y, NAME) {
sorted = this._precomputeYUIDependencies(langName, env, mojit, modules, required);
for (l = 0; l < langNames.length; l += 1) {
langName = langNames[l];
- lang = langs[langName];
langSorted = Y.mojito.util.copy(sorted);
- this._addLangToSorted(env, langSorted, lang);
+ this._addLangsToSorted(env, langSorted, langName, langRess);
this._setYUIDependencies(env, poslKey, langName, binder.yui.name, langSorted);
}
}
@@ -599,9 +599,11 @@ YUI.add('addon-rs-yui', function(Y, NAME) {
* @private
* @method _captureYUIModuleDetails
* @param {object} res resource metadata
+ * @param {object} runSandbox if passed, the function in the module
+ * will be called using this parameter as the YUI sandbox
* @return {nothing}
*/
- _captureYUIModuleDetails: function(res) {
+ _captureYUIModuleDetails: function(res, runSandbox) {
var file,
ctx,
yui = {};
@@ -623,6 +625,13 @@ YUI.add('addon-rs-yui', function(Y, NAME) {
if (!yui.meta.requires) {
yui.meta.requires = [];
}
+ if (runSandbox) {
+ try {
+ fn(runSandbox, yui.name);
+ } catch (e) {
+ Y.log('failed to run javascript file ' + res.source.fs.fullPath + '\n' + e.message, 'error', NAME);
+ }
+ }
}
}
};
@@ -643,16 +652,24 @@ YUI.add('addon-rs-yui', function(Y, NAME) {
* about the language.
*
* @private
- * @method _addLangToSorted
+ * @method _addLangsToSorted
* @param {string} env runtime environment (either `client`, or `server`)
* @param {object} sorted results of _precomputeYUIDependencies()
- * @param {object} langRes resource representing the language bundle
+ * @param {string} langName which language to add
+ * @param {object} langRess resources representing the language bundle
* @return {nothing} results are added to the `sorted` parameter
*/
- _addLangToSorted: function(env, sorted, langRes) {
- if (langRes) {
- sorted.sorted.push(langRes.yui.name);
- sorted.paths[langRes.yui.name] = ('client' === env) ? langRes.url : langRes.source.fs.fullPath;
+ _addLangsToSorted: function(env, sorted, langName, langRess) {
+ var modName,
+ langRes;
+ for (modName in langRess) {
+ if (langRess.hasOwnProperty(modName)) {
+ langRes = langRess[modName][langName] || langRess[modName][''];
+ if (langRes) {
+ sorted.sorted.push(langRes.yui.name);
+ sorted.paths[langRes.yui.name] = ('client' === env) ? langRes.url : langRes.source.fs.fullPath;
+ }
+ }
}
if (!sorted.paths.intl) {
sorted.sorted.unshift('intl');
@@ -1,3 +1,3 @@
-YUI.add('lang/testing_de', function(Y, NAME) {
+YUI.add('lang/testing', function(Y, NAME) {
// just needs to exist
});
@@ -276,24 +276,24 @@ YUI().use(
source = makeSource(fixtures, 'bundle', 'lang', 'testing.js', true);
res = store.parseResourceVersion(source, 'yui-lang', undefined, 'testing');
A.isNotUndefined(res);
- cmp(res.source, source);
- A.areSame('yui-lang', res.type);
- A.areSame('common', res.affinity);
- A.areSame('*', res.selector);
- A.areSame('', res.name);
- A.areSame('yui-lang--', res.id);
- A.areSame('testing', res.mojit);
+ cmp(res.source, source, 'testing.js source');
+ A.areSame('yui-lang', res.type, 'testing.js type');
+ A.areSame('common', res.affinity, 'testing.js affinity');
+ A.areSame('*', res.selector, 'testing.js selector');
+ A.areSame('lang/testing', res.name, 'testing.js name');
+ A.areSame('yui-lang--lang/testing', res.id, 'testing.js id');
+ A.areSame('testing', res.mojit, 'testing.js mojit');
source = makeSource(fixtures, 'bundle', 'lang', 'testing_de.js', true);
res = store.parseResourceVersion(source, 'yui-lang', undefined, 'testing');
A.isNotUndefined(res);
- cmp(res.source, source);
- A.areSame('yui-lang', res.type);
- A.areSame('common', res.affinity);
- A.areSame('*', res.selector);
- A.areSame('de', res.name);
- A.areSame('yui-lang--de', res.id);
- A.areSame('testing', res.mojit);
+ cmp(res.source, source, 'testing_de.js source');
+ A.areSame('yui-lang', res.type, 'testing_de.js type');
+ A.areSame('common', res.affinity, 'testing_de.js affinity');
+ A.areSame('*', res.selector, 'testing_de.js selector');
+ A.areSame('lang/testing_de', res.name, 'testing_de.js name');
+ A.areSame('yui-lang--lang/testing_de', res.id, 'testing_de.js id');
+ A.areSame('testing', res.mojit, 'testing_de.js mojit');
source = makeSource(fixtures, 'bundle', 'lang', 'testing_en-US.js', true);
res = store.parseResourceVersion(source, 'yui-lang', undefined, 'testing');
@@ -302,8 +302,8 @@ YUI().use(
A.areSame('yui-lang', res.type);
A.areSame('common', res.affinity);
A.areSame('*', res.selector);
- A.areSame('en-US', res.name);
- A.areSame('yui-lang--en-US', res.id);
+ A.areSame('lang/testing_en-US', res.name);
+ A.areSame('yui-lang--lang/testing_en-US', res.id);
A.areSame('testing', res.mojit);
},
@@ -404,40 +404,40 @@ YUI().use(
case 'spec--x':
case 'view--x':
break;
- case 'yui-lang--':
+ case 'yui-lang--lang/testing':
A.areSame(pkg, res.source.pkg);
A.areSame('yui-lang', res.type);
- A.areSame('', res.name);
+ A.areSame('lang/testing', res.name);
A.areSame('*', res.selector);
A.areSame('common', res.affinity);
A.areSame('.', res.source.fs.subDir);
A.areSame('testing', res.source.fs.basename);
A.areSame('.js', res.source.fs.ext);
break;
- case 'yui-lang--de':
+ case 'yui-lang--lang/testing_de':
A.areSame(pkg, res.source.pkg);
A.areSame('yui-lang', res.type);
- A.areSame('de', res.name);
+ A.areSame('lang/testing_de', res.name);
A.areSame('*', res.selector);
A.areSame('common', res.affinity);
A.areSame('.', res.source.fs.subDir);
A.areSame('testing_de', res.source.fs.basename);
A.areSame('.js', res.source.fs.ext);
break;
- case 'yui-lang--en':
+ case 'yui-lang--lang/testing_en':
A.areSame(pkg, res.source.pkg);
A.areSame('yui-lang', res.type);
- A.areSame('en', res.name);
+ A.areSame('lang/testing_en', res.name);
A.areSame('*', res.selector);
A.areSame('common', res.affinity);
A.areSame('.', res.source.fs.subDir);
A.areSame('testing_en', res.source.fs.basename);
A.areSame('.js', res.source.fs.ext);
break;
- case 'yui-lang--en-US':
+ case 'yui-lang--lang/testing_en-US':
A.areSame(pkg, res.source.pkg);
A.areSame('yui-lang', res.type);
- A.areSame('en-US', res.name);
+ A.areSame('lang/testing_en-US', res.name);
A.areSame('*', res.selector);
A.areSame('common', res.affinity);
A.areSame('.', res.source.fs.subDir);