i18n.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. //
  7. // dojo i18n! plugin
  8. //
  9. // We choose to include our own plugin in hopes of leveraging functionality already contained in dojo
  10. // and thereby reducing the size of the plugin compared to various loader implementations. Naturally, this
  11. // allows AMD loaders to be used without their plugins.
  12. // CAUTION, this module may return improper results if the AMD loader does not support toAbsMid and client
  13. // code passes relative plugin resource module ids. In that case, you should consider using the i18n! plugin
  14. // that comes with your loader.
  15. define(["dojo"], function(dojo) {
  16. var
  17. nlsRe=
  18. // regexp for reconstructing the master bundle name from parts of the regexp match
  19. // nlsRe.exec("foo/bar/baz/nls/en-ca/foo") gives:
  20. // ["foo/bar/baz/nls/en-ca/foo", "foo/bar/baz/nls/", "/", "/", "en-ca", "foo"]
  21. // nlsRe.exec("foo/bar/baz/nls/foo") gives:
  22. // ["foo/bar/baz/nls/foo", "foo/bar/baz/nls/", "/", "/", "foo", ""]
  23. // so, if match[5] is blank, it means this is the top bundle definition.
  24. // courtesy of http://requirejs.org
  25. /(^.*(^|\/)nls(\/|$))([^\/]*)\/?([^\/]*)/,
  26. getAvailableLocales= function(
  27. root,
  28. locale,
  29. bundlePath,
  30. bundleName
  31. ){
  32. // return a vector of module ids containing all available locales with respect to the target locale
  33. // For example, assuming:
  34. // * the root bundle indicates specific bundles for "fr" and "fr-ca",
  35. // * bundlePath is "myPackage/nls"
  36. // * bundleName is "myBundle"
  37. // Then a locale argument of "fr-ca" would return
  38. // ["myPackage/nls/myBundle", "myPackage/nls/fr/myBundle", "myPackage/nls/fr-ca/myBundle"]
  39. // Notice that bundles are returned least-specific to most-specific, starting with the root.
  40. for(var result= [bundlePath + bundleName], localeParts= locale.split("-"), current= "", i= 0; i<localeParts.length; i++){
  41. current+= localeParts[i];
  42. if(root[current]){
  43. result.push(bundlePath + current + "/" + bundleName);
  44. }
  45. }
  46. return result;
  47. },
  48. cache= {};
  49. return {
  50. load: function(id, require, load){
  51. // note: id may be relative
  52. var
  53. match= nlsRe.exec(id),
  54. bundlePath= (require.toAbsMid && require.toAbsMid(match[1])) || match[1],
  55. bundleName= match[5] || match[4],
  56. bundlePathAndName= bundlePath + bundleName,
  57. locale= (match[5] && match[4]) || dojo.locale,
  58. target= bundlePathAndName + "/" + locale;
  59. // if we've already resolved this request, just return it
  60. if (cache[target]) {
  61. load(cache[target]);
  62. return;
  63. }
  64. // get the root bundle which instructs which other bundles are required to contruct the localized bundle
  65. require([bundlePathAndName], function(root){
  66. var
  67. current= cache[bundlePathAndName + "/"]= dojo.clone(root.root),
  68. availableLocales= getAvailableLocales(root, locale, bundlePath, bundleName);
  69. require(availableLocales, function(){
  70. for (var i= 1; i<availableLocales.length; i++){
  71. cache[bundlePathAndName + "/" + availableLocales[i]]= current= dojo.mixin(dojo.clone(current), arguments[i]);
  72. }
  73. // target may not have been resolve (e.g., maybe only "fr" exists when "fr-ca" was requested)
  74. cache[target]= current;
  75. load(current);
  76. });
  77. });
  78. },
  79. cache: function(mid, value){
  80. cache[mid]= value;
  81. }
  82. };
  83. });