3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('widget-locale', function(Y) {
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();
86
Y.log("getStrings: For " + locale, "info", "widget");
88
var defLocale = this.getDefaultLocale().toLowerCase(),
89
defStrs = this._getStrings(defLocale),
90
strs = (defStrs) ? Y.merge(defStrs) : {},
91
localeSegments = locale.split(HYPHEN),
96
// If locale is different than the default, or needs lookup support
97
if (locale !== defLocale || localeSegments.length > 1) {
99
for (i = 0, l = localeSegments.length; i < l; ++i) {
100
lookup += localeSegments[i];
102
Y.log("getStrings: Merging in strings from: " + lookup, "info", "widget");
104
localeStrs = this._getStrings(lookup);
106
Y.aggregate(strs, localeStrs, TRUE);
116
* Gets the string for a particular key, for a particular locale, performing locale lookup.
118
* If no values if defined for the key, for the given locale, the value for the
119
* default locale (in initial locale set for the class) is returned.
122
* @param {String} key The key.
123
* @param {String} locale (optional) The locale for which the string value is required. Defaults to the current locale, if not provided.
125
getString : function(key, locale) {
127
locale = (locale || this.get(LOCALE)).toLowerCase();
129
Y.log("getString: For " + locale, "info", "widget");
131
var defLocale = (this.getDefaultLocale()).toLowerCase(),
132
strs = this._getStrings(defLocale) || {},
134
idx = locale.lastIndexOf(HYPHEN);
136
// If locale is different than the default, or needs lookup support
137
if (locale !== defLocale || idx != -1) {
139
Y.log("getString: Performing lookup for: " + locale, "info", "widget");
141
strs = this._getStrings(locale);
142
if (strs && key in strs) {
146
idx = locale.lastIndexOf(HYPHEN);
147
// Chop of last locale segment
149
locale = locale.substring(0, idx);
159
* Returns the default locale for the widget (the locale value defined by the
160
* widget class, or provided by the user during construction).
162
* @method getDefaultLocale
163
* @return {String} The default locale for the widget
165
getDefaultLocale : function() {
166
return this._state.get(LOCALE, INIT_VALUE);
169
_strSetter : function(val) {
170
return this._setStrings(val, this.get(LOCALE));
173
_strGetter : function(val) {
174
return this._getStrings(this.get(LOCALE));
179
}, '3.4.1' ,{requires:['widget-base']});