MDL-35616 import YUI 3.7.2
[moodle.git] / lib / yuilib / 3.7.2 / build / widget-locale / widget-locale.js
blob9c7ec85d7f8f6f48c4f8297ac3a0a85a497eea95
1 /*
2 YUI 3.7.2 (build 5639)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('widget-locale', function (Y, NAME) {
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;
22 /**
23  * @attribute locale
24  * @deprecated Use Y.config.lang and Y.Intl externalization support
25  * @description
26  * The default locale for the widget. NOTE: Using get/set on the "strings" attribute will
27  * return/set strings for this locale.
28  * @default "en"
29  * @type String
30  */
31 Widget.ATTRS[LOCALE] = {
32     value: "en"
35 // Since strings support with locale needs the private _strs setup
36 Widget.ATTRS.strings.lazyAdd = false;
38 Y.mix(Widget.prototype, {
40     /**
41      * Sets strings for a particular locale, merging with any existing
42      * strings which may already be defined for the locale.
43      *
44      * @method _setStrings
45      * @protected
46      * @param {Object} strings The hash of string key/values to set
47      * @param {Object} locale The locale for the string values being set
48      */
49     _setStrings : function(strings, locale) {
50         var strs = this._strs;
51         locale = locale.toLowerCase();
53         if (!strs[locale]) {
54             strs[locale] = {};
55         }
57         Y.aggregate(strs[locale], strings, TRUE);
58         return strs[locale];
59     },
61     /**
62      * Returns the strings key/value hash for a paricular locale, without locale lookup applied.
63      *
64      * @method _getStrings
65      * @protected
66      * @param {Object} locale
67      */
68     _getStrings : function(locale) {
69         return this._strs[locale.toLowerCase()];
70     },
72     /**
73      * Gets the entire strings hash for a particular locale, performing locale lookup.
74      * <p>
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.
77      * </p>
78      * @method getStrings
79      * @param {String} locale (optional) The locale for which the string value is required. Defaults to the current locale, if not provided.
80      */
81     // TODO: Optimize/Cache. Clear cache on _setStrings call.
82     getStrings : function(locale) {
83     
84         locale = (locale || this.get(LOCALE)).toLowerCase();
85     
86     
87         var defLocale = this.getDefaultLocale().toLowerCase(),
88             defStrs = this._getStrings(defLocale),
89             strs = (defStrs) ? Y.merge(defStrs) : {},
90             localeSegments = locale.split(HYPHEN),
91             localeStrs,
92             i, l,
93             lookup;
94     
95         // If locale is different than the default, or needs lookup support
96         if (locale !== defLocale || localeSegments.length > 1) {
97             lookup = EMPTY_STR;
98             for (i = 0, l = localeSegments.length; i < l; ++i) {
99                 lookup += localeSegments[i];
100     
101     
102                 localeStrs = this._getStrings(lookup);
103                 if (localeStrs) {
104                     Y.aggregate(strs, localeStrs, TRUE);
105                 }
106                 lookup += HYPHEN;
107             }
108         }
109     
110         return strs;
111     },
112     
113     /**
114      * Gets the string for a particular key, for a particular locale, performing locale lookup.
115      * <p>
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.
118      * </p>
119      * @method getString
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.
122      */
123     getString : function(key, locale) {
125         locale = (locale || this.get(LOCALE)).toLowerCase();
126     
127     
128         var defLocale = (this.getDefaultLocale()).toLowerCase(),
129             strs = this._getStrings(defLocale) || {},
130             str = strs[key],
131             idx = locale.lastIndexOf(HYPHEN);
132     
133         // If locale is different than the default, or needs lookup support
134         if (locale !== defLocale || idx != -1) {
135             do {
136     
137                 strs = this._getStrings(locale);
138                 if (strs && key in strs) {
139                     str = strs[key];
140                     break;
141                 }
142                 idx = locale.lastIndexOf(HYPHEN);
143                 // Chop of last locale segment
144                 if (idx != -1) {
145                     locale = locale.substring(0, idx);
146                 }
147     
148             } while (idx != -1);
149         }
150     
151         return str;
152     },
154     /**
155      * Returns the default locale for the widget (the locale value defined by the
156      * widget class, or provided by the user during construction).
157      *
158      * @method getDefaultLocale
159      * @return {String} The default locale for the widget
160      */
161     getDefaultLocale : function() {
162         return this._state.get(LOCALE, INIT_VALUE);
163     },
164     
165     _strSetter : function(val) {
166         return this._setStrings(val, this.get(LOCALE));
167     },
169     _strGetter : function(val) {
170         return this._getStrings(this.get(LOCALE));
171     }
172 }, true);
175 }, '3.7.2', {"requires": ["widget-base"]});