cldr.unresolved.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * CLDR JavaScript Library v0.4.3
  3. * http://jquery.com/
  4. *
  5. * Copyright 2013 Rafael Xavier de Souza
  6. * Released under the MIT license
  7. * http://jquery.org/license
  8. *
  9. * Date: 2015-08-24T01:00Z
  10. */
  11. /*!
  12. * CLDR JavaScript Library v0.4.3 2015-08-24T01:00Z MIT license © Rafael Xavier
  13. * http://git.io/h4lmVg
  14. */
  15. (function( factory ) {
  16. if ( typeof define === "function" && define.amd ) {
  17. // AMD.
  18. define( [ "../cldr" ], factory );
  19. } else if ( typeof module === "object" && typeof module.exports === "object" ) {
  20. // Node. CommonJS.
  21. module.exports = factory( require( "cldrjs" ) );
  22. } else {
  23. // Global
  24. factory( Cldr );
  25. }
  26. }(function( Cldr ) {
  27. // Build optimization hack to avoid duplicating functions across modules.
  28. var coreLoad = Cldr._coreLoad;
  29. var jsonMerge = Cldr._jsonMerge;
  30. var pathNormalize = Cldr._pathNormalize;
  31. var resourceGet = Cldr._resourceGet;
  32. var validatePresence = Cldr._validatePresence;
  33. var validateTypePath = Cldr._validateTypePath;
  34. var bundleParentLookup = function( Cldr, locale ) {
  35. var normalizedPath, parent;
  36. if ( locale === "root" ) {
  37. return;
  38. }
  39. // First, try to find parent on supplemental data.
  40. normalizedPath = pathNormalize( [ "supplemental/parentLocales/parentLocale", locale ] );
  41. parent = resourceGet( Cldr._resolved, normalizedPath ) || resourceGet( Cldr._raw, normalizedPath );
  42. if ( parent ) {
  43. return parent;
  44. }
  45. // Or truncate locale.
  46. parent = locale.substr( 0, locale.lastIndexOf( Cldr.localeSep ) );
  47. if ( !parent ) {
  48. return "root";
  49. }
  50. return parent;
  51. };
  52. // @path: normalized path
  53. var resourceSet = function( data, path, value ) {
  54. var i,
  55. node = data,
  56. length = path.length;
  57. for ( i = 0; i < length - 1; i++ ) {
  58. if ( !node[ path[ i ] ] ) {
  59. node[ path[ i ] ] = {};
  60. }
  61. node = node[ path[ i ] ];
  62. }
  63. node[ path[ i ] ] = value;
  64. };
  65. var itemLookup = (function() {
  66. var lookup;
  67. lookup = function( Cldr, locale, path, attributes, childLocale ) {
  68. var normalizedPath, parent, value;
  69. // 1: Finish recursion
  70. // 2: Avoid infinite loop
  71. if ( typeof locale === "undefined" /* 1 */ || locale === childLocale /* 2 */ ) {
  72. return;
  73. }
  74. // Resolve path
  75. normalizedPath = pathNormalize( path, attributes );
  76. // Check resolved (cached) data first
  77. // 1: Due to #16, never use the cached resolved non-leaf nodes. It may not
  78. // represent its leafs in its entirety.
  79. value = resourceGet( Cldr._resolved, normalizedPath );
  80. if ( value && typeof value !== "object" /* 1 */ ) {
  81. return value;
  82. }
  83. // Check raw data
  84. value = resourceGet( Cldr._raw, normalizedPath );
  85. if ( !value ) {
  86. // Or, lookup at parent locale
  87. parent = bundleParentLookup( Cldr, locale );
  88. value = lookup( Cldr, parent, path, jsonMerge( attributes, { bundle: parent }), locale );
  89. }
  90. if ( value ) {
  91. // Set resolved (cached)
  92. resourceSet( Cldr._resolved, normalizedPath, value );
  93. }
  94. return value;
  95. };
  96. return lookup;
  97. }());
  98. Cldr._raw = {};
  99. /**
  100. * Cldr.load( json [, json, ...] )
  101. *
  102. * @json [JSON] CLDR data or [Array] Array of @json's.
  103. *
  104. * Load resolved or unresolved cldr data.
  105. * Overwrite Cldr.load().
  106. */
  107. Cldr.load = function() {
  108. Cldr._raw = coreLoad( Cldr, Cldr._raw, arguments );
  109. };
  110. /**
  111. * Overwrite Cldr.prototype.get().
  112. */
  113. Cldr.prototype.get = function( path ) {
  114. validatePresence( path, "path" );
  115. validateTypePath( path, "path" );
  116. // 1: use bundle as locale on item lookup for simplification purposes, because no other extended subtag is used anyway on bundle parent lookup.
  117. // 2: during init(), this method is called, but bundle is yet not defined. Use "" as a workaround in this very specific scenario.
  118. return itemLookup( Cldr, this.attributes && this.attributes.bundle /* 1 */ || "" /* 2 */, path, this.attributes );
  119. };
  120. // In case cldr/unresolved is loaded after cldr/event, we trigger its overloads again. Because, .get is overwritten in here.
  121. if ( Cldr._eventInit ) {
  122. Cldr._eventInit();
  123. }
  124. return Cldr;
  125. }));