|
525
|
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 |
|
|
|
91 |
var defLocale = this.getDefaultLocale().toLowerCase(), |
|
|
92 |
defStrs = this._getStrings(defLocale), |
|
|
93 |
strs = (defStrs) ? Y.merge(defStrs) : {}, |
|
|
94 |
localeSegments = locale.split(HYPHEN), |
|
|
95 |
localeStrs, |
|
|
96 |
i, l, |
|
|
97 |
lookup; |
|
|
98 |
|
|
|
99 |
// If locale is different than the default, or needs lookup support |
|
|
100 |
if (locale !== defLocale || localeSegments.length > 1) { |
|
|
101 |
lookup = EMPTY_STR; |
|
|
102 |
for (i = 0, l = localeSegments.length; i < l; ++i) { |
|
|
103 |
lookup += localeSegments[i]; |
|
|
104 |
|
|
|
105 |
|
|
|
106 |
localeStrs = this._getStrings(lookup); |
|
|
107 |
if (localeStrs) { |
|
|
108 |
Y.aggregate(strs, localeStrs, TRUE); |
|
|
109 |
} |
|
|
110 |
lookup += HYPHEN; |
|
|
111 |
} |
|
|
112 |
} |
|
|
113 |
|
|
|
114 |
return strs; |
|
|
115 |
}, |
|
|
116 |
|
|
|
117 |
/** |
|
|
118 |
* Gets the string for a particular key, for a particular locale, performing locale lookup. |
|
|
119 |
* <p> |
|
|
120 |
* If no values if defined for the key, for the given locale, the value for the |
|
|
121 |
* default locale (in initial locale set for the class) is returned. |
|
|
122 |
* </p> |
|
|
123 |
* @method getString |
|
|
124 |
* @param {String} key The key. |
|
|
125 |
* @param {String} locale (optional) The locale for which the string value is required. Defaults to the current locale, if not provided. |
|
|
126 |
*/ |
|
|
127 |
getString : function(key, locale) { |
|
|
128 |
|
|
|
129 |
locale = (locale || this.get(LOCALE)).toLowerCase(); |
|
|
130 |
|
|
|
131 |
|
|
|
132 |
var defLocale = (this.getDefaultLocale()).toLowerCase(), |
|
|
133 |
strs = this._getStrings(defLocale) || {}, |
|
|
134 |
str = strs[key], |
|
|
135 |
idx = locale.lastIndexOf(HYPHEN); |
|
|
136 |
|
|
|
137 |
// If locale is different than the default, or needs lookup support |
|
|
138 |
if (locale !== defLocale || idx != -1) { |
|
|
139 |
do { |
|
|
140 |
|
|
|
141 |
strs = this._getStrings(locale); |
|
|
142 |
if (strs && key in strs) { |
|
|
143 |
str = strs[key]; |
|
|
144 |
break; |
|
|
145 |
} |
|
|
146 |
idx = locale.lastIndexOf(HYPHEN); |
|
|
147 |
// Chop of last locale segment |
|
|
148 |
if (idx != -1) { |
|
|
149 |
locale = locale.substring(0, idx); |
|
|
150 |
} |
|
|
151 |
|
|
|
152 |
} while (idx != -1); |
|
|
153 |
} |
|
|
154 |
|
|
|
155 |
return str; |
|
|
156 |
}, |
|
|
157 |
|
|
|
158 |
/** |
|
|
159 |
* Returns the default locale for the widget (the locale value defined by the |
|
|
160 |
* widget class, or provided by the user during construction). |
|
|
161 |
* |
|
|
162 |
* @method getDefaultLocale |
|
|
163 |
* @return {String} The default locale for the widget |
|
|
164 |
*/ |
|
|
165 |
getDefaultLocale : function() { |
|
|
166 |
return this._state.get(LOCALE, INIT_VALUE); |
|
|
167 |
}, |
|
|
168 |
|
|
|
169 |
_strSetter : function(val) { |
|
|
170 |
return this._setStrings(val, this.get(LOCALE)); |
|
|
171 |
}, |
|
|
172 |
|
|
|
173 |
_strGetter : function(val) { |
|
|
174 |
return this._getStrings(this.get(LOCALE)); |
|
|
175 |
} |
|
|
176 |
}, true); |
|
|
177 |
|
|
|
178 |
|
|
|
179 |
}, '3.10.3', {"requires": ["widget-base"]}); |