Skip to content

Commit

Permalink
Add the possibility to build the cldr data to have only one file per
Browse files Browse the repository at this point in the history
locale to download.
  • Loading branch information
clmath authored and JCEmmons committed Jul 24, 2014
1 parent e24559d commit a9cc7c5
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions impl/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
46 changes: 33 additions & 13 deletions impl/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand All @@ -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);
});
}
Expand Down
51 changes: 47 additions & 4 deletions locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = {};
}
};
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit a9cc7c5

Please sign in to comment.