Dashboard sipadu mbip
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

widget-locale.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('widget-locale', function(Y) {
  9. /**
  10. * Provides string support for widget with BCP 47 language tag lookup. This module has been deprecated. It's replaced by the "intl" module which provides generic internationalization and BCP 47 language tag support with externalization.
  11. *
  12. * @module widget-locale
  13. * @deprecated This module has been deprecated. It's replaced by the "intl" module which provides generic internationalization and BCP 47 language tag support with externalization.
  14. */
  15. var TRUE = true,
  16. LOCALE = "locale",
  17. INIT_VALUE = "initValue",
  18. HYPHEN = "-",
  19. EMPTY_STR = "",
  20. Widget = Y.Widget;
  21. /**
  22. * @attribute locale
  23. * @deprecated Use Y.config.lang and Y.Intl externalization support
  24. * @description
  25. * The default locale for the widget. NOTE: Using get/set on the "strings" attribute will
  26. * return/set strings for this locale.
  27. * @default "en"
  28. * @type String
  29. */
  30. Widget.ATTRS[LOCALE] = {
  31. value: "en"
  32. };
  33. // Since strings support with locale needs the private _strs setup
  34. Widget.ATTRS.strings.lazyAdd = false;
  35. Y.mix(Widget.prototype, {
  36. /**
  37. * Sets strings for a particular locale, merging with any existing
  38. * strings which may already be defined for the locale.
  39. *
  40. * @method _setStrings
  41. * @protected
  42. * @param {Object} strings The hash of string key/values to set
  43. * @param {Object} locale The locale for the string values being set
  44. */
  45. _setStrings : function(strings, locale) {
  46. var strs = this._strs;
  47. locale = locale.toLowerCase();
  48. if (!strs[locale]) {
  49. strs[locale] = {};
  50. }
  51. Y.aggregate(strs[locale], strings, TRUE);
  52. return strs[locale];
  53. },
  54. /**
  55. * Returns the strings key/value hash for a paricular locale, without locale lookup applied.
  56. *
  57. * @method _getStrings
  58. * @protected
  59. * @param {Object} locale
  60. */
  61. _getStrings : function(locale) {
  62. return this._strs[locale.toLowerCase()];
  63. },
  64. /**
  65. * Gets the entire strings hash for a particular locale, performing locale lookup.
  66. * <p>
  67. * If no values of the key are defined for a particular locale the value for the
  68. * default locale (in initial locale set for the class) is returned.
  69. * </p>
  70. * @method getStrings
  71. * @param {String} locale (optional) The locale for which the string value is required. Defaults to the current locale, if not provided.
  72. */
  73. // TODO: Optimize/Cache. Clear cache on _setStrings call.
  74. getStrings : function(locale) {
  75. locale = (locale || this.get(LOCALE)).toLowerCase();
  76. var defLocale = this.getDefaultLocale().toLowerCase(),
  77. defStrs = this._getStrings(defLocale),
  78. strs = (defStrs) ? Y.merge(defStrs) : {},
  79. localeSegments = locale.split(HYPHEN),
  80. localeStrs,
  81. i, l,
  82. lookup;
  83. // If locale is different than the default, or needs lookup support
  84. if (locale !== defLocale || localeSegments.length > 1) {
  85. lookup = EMPTY_STR;
  86. for (i = 0, l = localeSegments.length; i < l; ++i) {
  87. lookup += localeSegments[i];
  88. localeStrs = this._getStrings(lookup);
  89. if (localeStrs) {
  90. Y.aggregate(strs, localeStrs, TRUE);
  91. }
  92. lookup += HYPHEN;
  93. }
  94. }
  95. return strs;
  96. },
  97. /**
  98. * Gets the string for a particular key, for a particular locale, performing locale lookup.
  99. * <p>
  100. * If no values if defined for the key, for the given locale, the value for the
  101. * default locale (in initial locale set for the class) is returned.
  102. * </p>
  103. * @method getString
  104. * @param {String} key The key.
  105. * @param {String} locale (optional) The locale for which the string value is required. Defaults to the current locale, if not provided.
  106. */
  107. getString : function(key, locale) {
  108. locale = (locale || this.get(LOCALE)).toLowerCase();
  109. var defLocale = (this.getDefaultLocale()).toLowerCase(),
  110. strs = this._getStrings(defLocale) || {},
  111. str = strs[key],
  112. idx = locale.lastIndexOf(HYPHEN);
  113. // If locale is different than the default, or needs lookup support
  114. if (locale !== defLocale || idx != -1) {
  115. do {
  116. strs = this._getStrings(locale);
  117. if (strs && key in strs) {
  118. str = strs[key];
  119. break;
  120. }
  121. idx = locale.lastIndexOf(HYPHEN);
  122. // Chop of last locale segment
  123. if (idx != -1) {
  124. locale = locale.substring(0, idx);
  125. }
  126. } while (idx != -1);
  127. }
  128. return str;
  129. },
  130. /**
  131. * Returns the default locale for the widget (the locale value defined by the
  132. * widget class, or provided by the user during construction).
  133. *
  134. * @method getDefaultLocale
  135. * @return {String} The default locale for the widget
  136. */
  137. getDefaultLocale : function() {
  138. return this._state.get(LOCALE, INIT_VALUE);
  139. },
  140. _strSetter : function(val) {
  141. return this._setStrings(val, this.get(LOCALE));
  142. },
  143. _strGetter : function(val) {
  144. return this._getStrings(this.get(LOCALE));
  145. }
  146. }, true);
  147. }, '3.4.0' ,{requires:['widget-base']});