From a9cc7c52bec11f2956c4c34ddfe7c763e53c3df3 Mon Sep 17 00:00:00 2001 From: clmath Date: Thu, 24 Jul 2014 11:06:53 -0500 Subject: [PATCH] Add the possibility to build the cldr data to have only one file per locale to download. --- README.md | 2 +- bower.json | 2 +- impl/common.js | 2 ++ impl/load.js | 46 ++++++++++++++++++++++++++++++++------------- locales.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++---- package.json | 2 +- 6 files changed, 85 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 26ae0996..64a764bd 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ for number formatting ( Intl.NumberFormat ) and date and time formatting ( Intl. Collation ( Intl.Collator ) is not currently supported. ## Status -No official release yet. Relatively stable development release at 0.2.0-dev. +No official release yet. Relatively stable development release at 0.2.1-dev. ## Licensing diff --git a/bower.json b/bower.json index de3e79c1..83bfabb8 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "ecma402", - "version": "0.2.0-dev", + "version": "0.2.1-dev", "dependencies": { "requirejs": ">=2.1.14", "requirejs-text": ">=2.0.12", diff --git a/impl/common.js b/impl/common.js index 19c7f66f..f53898e4 100644 --- a/impl/common.js +++ b/impl/common.js @@ -254,6 +254,8 @@ define(["./List", "./Record", */ DefaultLocale : function () { var result; + var global = (function () {return this; })(); + var navigator = global.navigator; if (navigator && this.isStructurallyValidLanguageTag(navigator.language)) { result = this.BestFitAvailableLocale(this.availableLocalesList, this .CanonicalizeLanguageTag(navigator.language)); diff --git a/impl/load.js b/impl/load.js index ec507a05..fd48554a 100644 --- a/impl/load.js +++ b/impl/load.js @@ -7,10 +7,13 @@ define([ "./calendars", "./common", "require", + "module", "requirejs-text/text" // just so builder knows we will be using that module -], function (calendars, common, require) { +], function (calendars, common, require, module) { return { - load: function (locale, callerRequire, onload) { + id: module.id, + + load: function (locale, callerRequire, onload, loaderConfig) { // Compute dependencies to require(). // For specified locale, load JSON files for its "currencies", "numbers" data. var jsonElements = ["currencies", "numbers"]; @@ -19,36 +22,53 @@ define([ var supportedCalendars = common._getSupportedCalendars(region); supportedCalendars.forEach(function (calendar) { var calendarName = "ca-" + (calendar === "gregory" ? "gregorian" : calendar); - if (jsonElements.indexOf(calendarName) === -1) { - jsonElements.push(calendarName); - } + // Add json data + jsonElements.push(calendarName); + // Add calendar module if (calendar !== "gregory") { calendarsToLoad.push(calendar); } }); - var dependencies = jsonElements.map(function (element) { - return "requirejs-text/text!../cldr/" + locale + "/" + element + ".json"; - }); + var dependencies; + + // Check if there is a layer + var config = module.config(); + if (config[locale]) { + dependencies = jsonElements = [config._layerMid + "_" + locale]; + } else { + dependencies = jsonElements.map(function (element) { + return "requirejs-text/text!../cldr/" + locale + "/" + element + ".json"; + }); + } var calendarDependencies = calendarsToLoad.map(function (calendar) { return "../calendars/" + calendars.dependencies[calendar].calendar; }); - calendarDependencies.forEach(function (dep) { - dependencies.push(dep); - }); + dependencies = dependencies.concat(calendarDependencies); // Load all the JSON files requested, and any non-gregorian calendars - // that are required. Return the locale data in a hash + // that are required. Return the locale data in a hash require(dependencies, function () { var dataAsArray = arguments, dataAsHash = {}; jsonElements.forEach(function (element, idx) { - dataAsHash[element] = JSON.parse(dataAsArray[idx]); + if (config[locale]) { + dataAsHash = dataAsArray[0]; + } else { + dataAsHash[element] = JSON.parse(dataAsArray[idx]); + } }); calendarsToLoad.forEach(function (cal, idx) { calendars.calendarMap[cal] = dataAsArray[idx + jsonElements.length]; }); + + if (loaderConfig.isBuild) { + dataAsHash.calendars = calendarsToLoad.map(function (calendar) { + return calendars.dependencies[calendar].calendar; + }); + } + onload(dataAsHash); }); } diff --git a/locales.js b/locales.js index a821fe1a..78821cf0 100644 --- a/locales.js +++ b/locales.js @@ -11,8 +11,11 @@ define([ "module", "require", "./impl/common", - "./impl/load" // just so builder knows we will be using that module -], function (module, require, common) { + "./impl/load" +], function (module, require, common, loadCss) { + // Build variable + var writeFile; + // Compute locales to pre-load. Use hash to remove duplicates. var localeHash = {}; localeHash.root = true; @@ -38,20 +41,60 @@ define([ } } var locales = Object.keys(localeHash); + var localeDataHash = {}; // Compute dependencies to require() - var dependencies = locales.map(function (locale) { return "./impl/load!" + locale; }); + function getDependency(locale) { + return loadCss.id + "!" + locale; + } return { load: function (path, callerRequire, onload) { + var dependencies = locales.map(getDependency); // Load the locale data for every requested locale, and then return it in a hash require(dependencies, function () { - var localeDataArray = arguments, localeDataHash = {}; + var localeDataArray = arguments; locales.forEach(function (locale, idx) { localeDataHash[locale] = localeDataArray[idx]; }); onload(localeDataHash); }); + }, + + writeFile: function (pluginName, resource, callerRequire, write) { + writeFile = write; + }, + + addModules: function (pluginName, resource, addModules) { + var modulesToAdd = []; + locales.forEach(function (locale) { + var localeData = localeDataHash[locale]; + var calendarsDeps = localeData.calendars.map(function (cal) {return "./calendars/" + cal; }); + modulesToAdd = modulesToAdd.concat(calendarsDeps); + delete localeData.calendars; + }); + addModules(modulesToAdd); + }, + + onLayerEnd: function (write, data) { + // Calculate layer path + var match = data.path.match(/^(.*\/)?(.*)\.js$/); + var partialLayerPath = (match[1] || "") + "cldr/" + match[2] + "_"; + + // Calculate layer mid + match = data.name.match(/^(.*\/)?(.*)$/); + var layerMid = (match[1] || "") + "cldr/" + match[2]; + + locales.forEach(function (locale) { + var path = partialLayerPath + locale + ".js"; + writeFile(path, "define(" + JSON.stringify(localeDataHash[locale]) + ")"); + }); + + localeHash._layerMid = layerMid; + write("require.config({config:{'" + loadCss.id + "':" + JSON.stringify(localeHash) + "}});"); + + // Reset + localeDataHash = {}; } }; }); diff --git a/package.json b/package.json index 63253aef..8dbab368 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ecma402", - "version": "0.2.0-dev", + "version": "0.2.1-dev", "dependencies": { "requirejs": ">=2.1.14", "requirejs-dplugins": ">=0.2.2-dev"