locales.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Plugin to pre-load the locales the app has specified via:
  3. *
  4. * require.config({
  5. * config: {
  6. * "ecma402/locales": /^(ar-(TN|SA)|en|es|hi|ja|de)$/
  7. * }
  8. * });
  9. */
  10. define([
  11. "module",
  12. "require",
  13. "./impl/common",
  14. "./impl/load"
  15. ], function (module, require, common, loadCss) {
  16. // Build variable
  17. var writeFile;
  18. // Compute locales to pre-load. Use hash to remove duplicates.
  19. var localeHash = {};
  20. localeHash.root = true;
  21. localeHash[common.DefaultLocale()] = true;
  22. var config = module.config();
  23. if (config instanceof RegExp) {
  24. common.availableLocalesList.forEach(function (locale) {
  25. if (config.test(locale)) {
  26. localeHash[locale] = true;
  27. }
  28. });
  29. } else {
  30. if (typeof config === "string") {
  31. config = [ config ];
  32. }
  33. if (config instanceof Array) {
  34. config.forEach(function (locale) {
  35. var bestFitPreload = common.BestFitAvailableLocale(common.availableLocalesList, locale);
  36. if (bestFitPreload) {
  37. localeHash[bestFitPreload] = true;
  38. }
  39. });
  40. }
  41. }
  42. var locales = Object.keys(localeHash);
  43. var localeDataHash = {};
  44. // Compute dependencies to require()
  45. function getDependency(locale) {
  46. return loadCss.id + "!" + locale;
  47. }
  48. return {
  49. load: function (path, callerRequire, onload, loaderConfig) {
  50. if (config instanceof Object && loaderConfig.isBuild) {
  51. localeHash = {};
  52. common.availableLocalesList.forEach(function (locale) {
  53. localeHash[locale] = true;
  54. });
  55. locales = Object.keys(localeHash);
  56. }
  57. var dependencies = locales.map(getDependency);
  58. // Load the locale data for every requested locale, and then return it in a hash
  59. require(dependencies, function () {
  60. var localeDataArray = arguments;
  61. locales.forEach(function (locale, idx) {
  62. localeDataHash[locale] = localeDataArray[idx];
  63. });
  64. onload(localeDataHash);
  65. });
  66. },
  67. writeFile: function (pluginName, resource, callerRequire, write) {
  68. writeFile = write;
  69. },
  70. addModules: function (pluginName, resource, addModules) {
  71. var modulesToAdd = [];
  72. locales.forEach(function (locale) {
  73. var localeData = localeDataHash[locale];
  74. var calendarsDeps = localeData.calendars.map(function (cal) {return "./calendars/" + cal; });
  75. modulesToAdd = modulesToAdd.concat(calendarsDeps);
  76. delete localeData.calendars;
  77. });
  78. addModules(modulesToAdd);
  79. },
  80. onLayerEnd: function (write, data) {
  81. // Calculate layer path
  82. var match = data.path.match(/^(.*\/)?(.*)\.js$/);
  83. var partialLayerPath = (match[1] || "") + "cldr/" + match[2] + "_";
  84. // Calculate layer mid
  85. match = data.name.match(/^(.*\/)?(.*)$/);
  86. var layerMid = (match[1] || "") + "cldr/" + match[2];
  87. locales.forEach(function (locale) {
  88. var path = partialLayerPath + locale + ".js";
  89. writeFile(path, "define(" + JSON.stringify(localeDataHash[locale]) + ")");
  90. });
  91. localeHash._layerMid = layerMid;
  92. write("require.config({config:{'" + loadCss.id + "':" + JSON.stringify(localeHash) + "}});");
  93. // Reset
  94. localeDataHash = {};
  95. }
  96. };
  97. });