2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('widget-locale', function(Y) {
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.
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.
18 INIT_VALUE = "initValue",
25 * @deprecated Use Y.config.lang and Y.Intl externalization support
27 * The default locale for the widget. NOTE: Using get/set on the "strings" attribute will
28 * return/set strings for this locale.
32 Widget.ATTRS[LOCALE] = {
36 // Since strings support with locale needs the private _strs setup
37 Widget.ATTRS.strings.lazyAdd = false;
39 Y.mix(Widget.prototype, {
42 * Sets strings for a particular locale, merging with any existing
43 * strings which may already be defined for the locale.
47 * @param {Object} strings The hash of string key/values to set
48 * @param {Object} locale The locale for the string values being set
50 _setStrings : function(strings, locale) {
51 var strs = this._strs;
52 locale = locale.toLowerCase();
58 Y.aggregate(strs[locale], strings, TRUE);
63 * Returns the strings key/value hash for a paricular locale, without locale lookup applied.
67 * @param {Object} locale
69 _getStrings : function(locale) {
70 return this._strs[locale.toLowerCase()];
74 * Gets the entire strings hash for a particular locale, performing locale lookup.
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.
80 * @param {String} locale (optional) The locale for which the string value is required. Defaults to the current locale, if not provided.
82 // TODO: Optimize/Cache. Clear cache on _setStrings call.
83 getStrings : function(locale) {
85 locale = (locale || this.get(LOCALE)).toLowerCase();
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];
103 localeStrs = this._getStrings(lookup);
105 Y.aggregate(strs, localeStrs, TRUE);
115 * Gets the string for a particular key, for a particular locale, performing locale lookup.
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.
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.
124 getString : function(key, locale) {
126 locale = (locale || this.get(LOCALE)).toLowerCase();
129 var defLocale = (this.getDefaultLocale()).toLowerCase(),
130 strs = this._getStrings(defLocale) || {},
132 idx = locale.lastIndexOf(HYPHEN);
134 // If locale is different than the default, or needs lookup support
135 if (locale !== defLocale || idx != -1) {
138 strs = this._getStrings(locale);
139 if (strs && key in strs) {
143 idx = locale.lastIndexOf(HYPHEN);
144 // Chop of last locale segment
146 locale = locale.substring(0, idx);
156 * Returns the default locale for the widget (the locale value defined by the
157 * widget class, or provided by the user during construction).
159 * @method getDefaultLocale
160 * @return {String} The default locale for the widget
162 getDefaultLocale : function() {
163 return this._state.get(LOCALE, INIT_VALUE);
166 _strSetter : function(val) {
167 return this._setStrings(val, this.get(LOCALE));
170 _strGetter : function(val) {
171 return this._getStrings(this.get(LOCALE));
176 }, '3.3.0' ,{requires:['widget-base']});