]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/widget/widget-locale.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / widget / widget-locale.js
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.3.0
6 build: 3167
7 */
8 YUI.add('widget-locale', function(Y) {
9
10 /**
11  * 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  *
13  * @module widget-locale
14  * @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.
15  */
16 var TRUE = true,
17     LOCALE = "locale",
18     INIT_VALUE = "initValue",
19     HYPHEN = "-",
20     EMPTY_STR = "",
21     Widget = Y.Widget;
22
23 /**
24  * @attribute locale
25  * @deprecated Use Y.config.lang and Y.Intl externalization support
26  * @description
27  * The default locale for the widget. NOTE: Using get/set on the "strings" attribute will
28  * return/set strings for this locale.
29  * @default "en"
30  * @type String
31  */
32 Widget.ATTRS[LOCALE] = {
33     value: "en"
34 };
35
36 // Since strings support with locale needs the private _strs setup
37 Widget.ATTRS.strings.lazyAdd = false;
38
39 Y.mix(Widget.prototype, {
40
41     /**
42      * Sets strings for a particular locale, merging with any existing
43      * strings which may already be defined for the locale.
44      *
45      * @method _setStrings
46      * @protected
47      * @param {Object} strings The hash of string key/values to set
48      * @param {Object} locale The locale for the string values being set
49      */
50     _setStrings : function(strings, locale) {
51         var strs = this._strs;
52         locale = locale.toLowerCase();
53
54         if (!strs[locale]) {
55             strs[locale] = {};
56         }
57
58         Y.aggregate(strs[locale], strings, TRUE);
59         return strs[locale];
60     },
61
62     /**
63      * Returns the strings key/value hash for a paricular locale, without locale lookup applied.
64      *
65      * @method _getStrings
66      * @protected
67      * @param {Object} locale
68      */
69     _getStrings : function(locale) {
70         return this._strs[locale.toLowerCase()];
71     },
72
73     /**
74      * Gets the entire strings hash for a particular locale, performing locale lookup.
75      * <p>
76      * If no values of the key are defined for a particular locale the value for the 
77      * default locale (in initial locale set for the class) is returned.
78      * </p>
79      * @method getStrings
80      * @param {String} locale (optional) The locale for which the string value is required. Defaults to the current locale, if not provided.
81      */
82     // TODO: Optimize/Cache. Clear cache on _setStrings call.
83     getStrings : function(locale) {
84     
85         locale = (locale || this.get(LOCALE)).toLowerCase();
86     
87     
88         var defLocale = this.getDefaultLocale().toLowerCase(),
89             defStrs = this._getStrings(defLocale),
90             strs = (defStrs) ? Y.merge(defStrs) : {},
91             localeSegments = locale.split(HYPHEN),
92             localeStrs,
93             i, l,
94             lookup;
95     
96         // If locale is different than the default, or needs lookup support
97         if (locale !== defLocale || localeSegments.length > 1) {
98             lookup = EMPTY_STR;
99             for (i = 0, l = localeSegments.length; i < l; ++i) {
100                 lookup += localeSegments[i];
101     
102     
103                 localeStrs = this._getStrings(lookup);
104                 if (localeStrs) {
105                     Y.aggregate(strs, localeStrs, TRUE);
106                 }
107                 lookup += HYPHEN;
108             }
109         }
110     
111         return strs;
112     },
113     
114     /**
115      * Gets the string for a particular key, for a particular locale, performing locale lookup.
116      * <p>
117      * If no values if defined for the key, for the given locale, the value for the 
118      * default locale (in initial locale set for the class) is returned.
119      * </p>
120      * @method getString
121      * @param {String} key The key.
122      * @param {String} locale (optional) The locale for which the string value is required. Defaults to the current locale, if not provided.
123      */
124     getString : function(key, locale) {
125
126         locale = (locale || this.get(LOCALE)).toLowerCase();
127     
128     
129         var defLocale = (this.getDefaultLocale()).toLowerCase(),
130             strs = this._getStrings(defLocale) || {},
131             str = strs[key],
132             idx = locale.lastIndexOf(HYPHEN);
133     
134         // If locale is different than the default, or needs lookup support
135         if (locale !== defLocale || idx != -1) {
136             do {
137     
138                 strs = this._getStrings(locale);
139                 if (strs && key in strs) {
140                     str = strs[key];
141                     break;
142                 }
143                 idx = locale.lastIndexOf(HYPHEN);
144                 // Chop of last locale segment
145                 if (idx != -1) {
146                     locale = locale.substring(0, idx);
147                 }
148     
149             } while (idx != -1);
150         }
151     
152         return str;
153     },
154
155     /**
156      * Returns the default locale for the widget (the locale value defined by the
157      * widget class, or provided by the user during construction).
158      *
159      * @method getDefaultLocale
160      * @return {String} The default locale for the widget
161      */
162     getDefaultLocale : function() {
163         return this._state.get(LOCALE, INIT_VALUE);
164     },
165     
166     _strSetter : function(val) {
167         return this._setStrings(val, this.get(LOCALE));
168     },
169
170     _strGetter : function(val) {
171         return this._getStrings(this.get(LOCALE));
172     }
173 }, true);
174
175
176 }, '3.3.0' ,{requires:['widget-base']});