timezonelist.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Functions for listing timezone names.
  16. * @suppress {deprecated} Use goog.i18n instead.
  17. */
  18. /** @suppress {extraProvide} */
  19. goog.provide('goog.locale.TimeZoneList');
  20. goog.require('goog.locale');
  21. /**
  22. * Returns the displayable list of short timezone names paired with its id for
  23. * the current locale, selected based on the region or language provided.
  24. *
  25. * This method depends on `goog.locale.TimeZone*__<locale>` available
  26. * from http://go/js_locale_data. Users of this method must add a dependency on
  27. * this.
  28. *
  29. * @param {string=} opt_regionOrLang If region tag is provided, timezone ids
  30. * specific this region are considered. If language is provided, all regions
  31. * for which this language is defacto official is considered. If
  32. * this parameter is not speficied, current locale is used to
  33. * extract this information.
  34. *
  35. * @return {!Array<Object>} Localized and relevant list of timezone names
  36. * and ids.
  37. */
  38. goog.locale.getTimeZoneSelectedShortNames = function(opt_regionOrLang) {
  39. return goog.locale.getTimeZoneNameList_(
  40. 'TimeZoneSelectedShortNames', opt_regionOrLang);
  41. };
  42. /**
  43. * Returns the displayable list of long timezone names paired with its id for
  44. * the current locale, selected based on the region or language provided.
  45. *
  46. * This method depends on `goog.locale.TimeZone*__<locale>` available
  47. * from http://go/js_locale_data. Users of this method must add a dependency on
  48. * this.
  49. *
  50. * @param {string=} opt_regionOrLang If region tag is provided, timezone ids
  51. * specific this region are considered. If language is provided, all regions
  52. * for which this language is defacto official is considered. If
  53. * this parameter is not speficied, current locale is used to
  54. * extract this information.
  55. *
  56. * @return {!Array<Object>} Localized and relevant list of timezone names
  57. * and ids.
  58. */
  59. goog.locale.getTimeZoneSelectedLongNames = function(opt_regionOrLang) {
  60. return goog.locale.getTimeZoneNameList_(
  61. 'TimeZoneSelectedLongNames', opt_regionOrLang);
  62. };
  63. /**
  64. * Returns the displayable list of long timezone names paired with its id for
  65. * the current locale.
  66. *
  67. * This method depends on `goog.locale.TimeZoneAllLongNames__<locale>` available
  68. * from http://go/js_locale_data. Users of this method must add a dependency on
  69. * this.
  70. *
  71. * @return {Array<Object>} localized and relevant list of timezone names
  72. * and ids.
  73. */
  74. goog.locale.getTimeZoneAllLongNames = function() {
  75. var locale = goog.locale.getLocale();
  76. return /** @type {Array<Object>} */ (
  77. goog.locale.getResource('TimeZoneAllLongNames', locale));
  78. };
  79. /**
  80. * Returns the displayable list of timezone names paired with its id for
  81. * the current locale, selected based on the region or language provided.
  82. *
  83. * This method depends on `goog.locale.TimeZone*__<locale>` available
  84. * from http://go/js_locale_data. Users of this method must add a dependency on
  85. * this.
  86. *
  87. * @param {string} nameType Resource name to be loaded to get the names.
  88. *
  89. * @param {string=} opt_resource If resource is region tag, timezone ids
  90. * specific this region are considered. If it is language, all regions
  91. * for which this language is defacto official is considered. If it is
  92. * undefined, current locale is used to extract this information.
  93. *
  94. * @return {!Array<Object>} Localized and relevant list of timezone names
  95. * and ids.
  96. * @private
  97. */
  98. goog.locale.getTimeZoneNameList_ = function(nameType, opt_resource) {
  99. var locale = goog.locale.getLocale();
  100. if (!opt_resource) {
  101. opt_resource = goog.locale.getRegionSubTag(locale);
  102. }
  103. // if there is no region subtag, use the language itself as the resource
  104. if (!opt_resource) {
  105. opt_resource = locale;
  106. }
  107. var names = goog.locale.getResource(nameType, locale);
  108. var ids = goog.locale.getResource('TimeZoneSelectedIds', opt_resource);
  109. var len = ids.length;
  110. var result = [];
  111. for (var i = 0; i < len; i++) {
  112. var id = ids[i];
  113. result.push({'id': id, 'name': names[id]});
  114. }
  115. return result;
  116. };