~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/yuilib/3.9.1/build/widget-locale/widget-locale.js

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

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