3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('widget-locale', function (Y, NAME) {
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.
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.
17 INIT_VALUE = "initValue",
24 * @deprecated Use Y.config.lang and Y.Intl externalization support
26 * The default locale for the widget. NOTE: Using get/set on the "strings" attribute will
27 * return/set strings for this locale.
31 Widget.ATTRS[LOCALE] = {
35 // Since strings support with locale needs the private _strs setup
36 Widget.ATTRS.strings.lazyAdd = false;
38 Y.mix(Widget.prototype, {
41 * Sets strings for a particular locale, merging with any existing
42 * strings which may already be defined for the locale.
46 * @param {Object} strings The hash of string key/values to set
47 * @param {Object} locale The locale for the string values being set
49 _setStrings : function(strings, locale) {
50 var strs = this._strs;
51 locale = locale.toLowerCase();
57 Y.aggregate(strs[locale], strings, TRUE);
62 * Returns the strings key/value hash for a paricular locale, without locale lookup applied.
66 * @param {Object} locale
68 _getStrings : function(locale) {
69 return this._strs[locale.toLowerCase()];
73 * Gets the entire strings hash for a particular locale, performing locale lookup.
75 * If no values of the key are defined for a particular locale the value for the
76 * default locale (in initial locale set for the class) is returned.
79 * @param {String} locale (optional) The locale for which the string value is required. Defaults to the current locale, if not provided.
81 // TODO: Optimize/Cache. Clear cache on _setStrings call.
82 getStrings : function(locale) {
84 locale = (locale || this.get(LOCALE)).toLowerCase();
87 var defLocale = this.getDefaultLocale().toLowerCase(),
88 defStrs = this._getStrings(defLocale),
89 strs = (defStrs) ? Y.merge(defStrs) : {},
90 localeSegments = locale.split(HYPHEN),
95 // If locale is different than the default, or needs lookup support
96 if (locale !== defLocale || localeSegments.length > 1) {
98 for (i = 0, l = localeSegments.length; i < l; ++i) {
99 lookup += localeSegments[i];
102 localeStrs = this._getStrings(lookup);
104 Y.aggregate(strs, localeStrs, TRUE);
114 * Gets the string for a particular key, for a particular locale, performing locale lookup.
116 * If no values if defined for the key, for the given locale, the value for the
117 * default locale (in initial locale set for the class) is returned.
120 * @param {String} key The key.
121 * @param {String} locale (optional) The locale for which the string value is required. Defaults to the current locale, if not provided.
123 getString : function(key, locale) {
125 locale = (locale || this.get(LOCALE)).toLowerCase();
128 var defLocale = (this.getDefaultLocale()).toLowerCase(),
129 strs = this._getStrings(defLocale) || {},
131 idx = locale.lastIndexOf(HYPHEN);
133 // If locale is different than the default, or needs lookup support
134 if (locale !== defLocale || idx != -1) {
137 strs = this._getStrings(locale);
138 if (strs && key in strs) {
142 idx = locale.lastIndexOf(HYPHEN);
143 // Chop of last locale segment
145 locale = locale.substring(0, idx);
155 * Returns the default locale for the widget (the locale value defined by the
156 * widget class, or provided by the user during construction).
158 * @method getDefaultLocale
159 * @return {String} The default locale for the widget
161 getDefaultLocale : function() {
162 return this._state.get(LOCALE, INIT_VALUE);
165 _strSetter : function(val) {
166 return this._setStrings(val, this.get(LOCALE));
169 _strGetter : function(val) {
170 return this._getStrings(this.get(LOCALE));
175 }, '3.7.2', {"requires": ["widget-base"]});