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