1 |
efrain |
1 |
YUI.add('intl', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
var _mods = {},
|
|
|
4 |
|
|
|
5 |
ROOT_LANG = "yuiRootLang",
|
|
|
6 |
ACTIVE_LANG = "yuiActiveLang",
|
|
|
7 |
NONE = [];
|
|
|
8 |
|
|
|
9 |
/**
|
|
|
10 |
* Provides utilities to support the management of localized resources (strings and formatting patterns).
|
|
|
11 |
*
|
|
|
12 |
* @module intl
|
|
|
13 |
*/
|
|
|
14 |
|
|
|
15 |
/**
|
|
|
16 |
* The Intl utility provides a central location for managing sets of localized resources (strings and formatting patterns).
|
|
|
17 |
*
|
|
|
18 |
* @class Intl
|
|
|
19 |
* @uses EventTarget
|
|
|
20 |
* @static
|
|
|
21 |
*/
|
|
|
22 |
Y.mix(Y.namespace("Intl"), {
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Private method to retrieve the language hash for a given module.
|
|
|
26 |
*
|
|
|
27 |
* @method _mod
|
|
|
28 |
* @private
|
|
|
29 |
*
|
|
|
30 |
* @param {String} module The name of the module
|
|
|
31 |
* @return {Object} The hash of localized resources for the module, keyed by BCP language tag
|
|
|
32 |
*/
|
|
|
33 |
_mod : function(module) {
|
|
|
34 |
if (!_mods[module]) {
|
|
|
35 |
_mods[module] = {};
|
|
|
36 |
}
|
|
|
37 |
return _mods[module];
|
|
|
38 |
},
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Sets the active language for the given module.
|
|
|
42 |
*
|
|
|
43 |
* Returns false on failure, which would happen if the language had not been registered through the <a href="#method_add">add()</a> method.
|
|
|
44 |
*
|
|
|
45 |
* @method setLang
|
|
|
46 |
*
|
|
|
47 |
* @param {String} module The module name.
|
|
|
48 |
* @param {String} lang The BCP 47 language tag.
|
|
|
49 |
* @return boolean true if successful, false if not.
|
|
|
50 |
*/
|
|
|
51 |
setLang : function(module, lang) {
|
|
|
52 |
var langs = this._mod(module),
|
|
|
53 |
currLang = langs[ACTIVE_LANG],
|
|
|
54 |
exists = !!langs[lang];
|
|
|
55 |
|
|
|
56 |
if (exists && lang !== currLang) {
|
|
|
57 |
langs[ACTIVE_LANG] = lang;
|
|
|
58 |
this.fire("intl:langChange", {module: module, prevVal: currLang, newVal: (lang === ROOT_LANG) ? "" : lang});
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
return exists;
|
|
|
62 |
},
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Get the currently active language for the given module.
|
|
|
66 |
*
|
|
|
67 |
* @method getLang
|
|
|
68 |
*
|
|
|
69 |
* @param {String} module The module name.
|
|
|
70 |
* @return {String} The BCP 47 language tag.
|
|
|
71 |
*/
|
|
|
72 |
getLang : function(module) {
|
|
|
73 |
var lang = this._mod(module)[ACTIVE_LANG];
|
|
|
74 |
return (lang === ROOT_LANG) ? "" : lang;
|
|
|
75 |
},
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Register a hash of localized resources for the given module and language
|
|
|
79 |
*
|
|
|
80 |
* @method add
|
|
|
81 |
*
|
|
|
82 |
* @param {String} module The module name.
|
|
|
83 |
* @param {String} lang The BCP 47 language tag.
|
|
|
84 |
* @param {Object} strings The hash of localized values, keyed by the string name.
|
|
|
85 |
*/
|
|
|
86 |
add : function(module, lang, strings) {
|
|
|
87 |
lang = lang || ROOT_LANG;
|
|
|
88 |
this._mod(module)[lang] = strings;
|
|
|
89 |
this.setLang(module, lang);
|
|
|
90 |
},
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Gets the module's localized resources for the currently active language (as provided by the <a href="#method_getLang">getLang</a> method).
|
|
|
94 |
* <p>
|
|
|
95 |
* Optionally, the localized resources for alternate languages which have been added to Intl (see the <a href="#method_add">add</a> method) can
|
|
|
96 |
* be retrieved by providing the BCP 47 language tag as the lang parameter.
|
|
|
97 |
* </p>
|
|
|
98 |
* @method get
|
|
|
99 |
*
|
|
|
100 |
* @param {String} module The module name.
|
|
|
101 |
* @param {String} key Optional. A single resource key. If not provided, returns a copy (shallow clone) of all resources.
|
|
|
102 |
* @param {String} lang Optional. The BCP 47 language tag. If not provided, the module's currently active language is used.
|
|
|
103 |
* @return String | Object A copy of the module's localized resources, or a single value if key is provided.
|
|
|
104 |
*/
|
|
|
105 |
get : function(module, key, lang) {
|
|
|
106 |
var mod = this._mod(module),
|
|
|
107 |
strs;
|
|
|
108 |
|
|
|
109 |
lang = lang || mod[ACTIVE_LANG];
|
|
|
110 |
strs = mod[lang] || {};
|
|
|
111 |
|
|
|
112 |
return (key) ? strs[key] : Y.merge(strs);
|
|
|
113 |
},
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Gets the list of languages for which localized resources are available for a given module, based on the module
|
|
|
117 |
* meta-data (part of loader). If loader is not on the page, returns an empty array.
|
|
|
118 |
*
|
|
|
119 |
* @method getAvailableLangs
|
|
|
120 |
* @param {String} module The name of the module
|
|
|
121 |
* @return {Array} The array of languages available.
|
|
|
122 |
*/
|
|
|
123 |
getAvailableLangs : function(module) {
|
|
|
124 |
var loader = Y.Env._loader,
|
|
|
125 |
mod = loader && loader.moduleInfo[module],
|
|
|
126 |
langs = mod && mod.lang;
|
|
|
127 |
return (langs) ? langs.concat() : NONE;
|
|
|
128 |
|
|
|
129 |
}
|
|
|
130 |
});
|
|
|
131 |
|
|
|
132 |
Y.augment(Y.Intl, Y.EventTarget);
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* Notification event to indicate when the lang for a module has changed. There is no default behavior associated with this event,
|
|
|
136 |
* so the on and after moments are equivalent.
|
|
|
137 |
*
|
|
|
138 |
* @event intl:langChange
|
|
|
139 |
* @param {EventFacade} e The event facade
|
|
|
140 |
* <p>The event facade contains:</p>
|
|
|
141 |
* <dl>
|
|
|
142 |
* <dt>module</dt><dd>The name of the module for which the language changed</dd>
|
|
|
143 |
* <dt>newVal</dt><dd>The new language tag</dd>
|
|
|
144 |
* <dt>prevVal</dt><dd>The current language tag</dd>
|
|
|
145 |
* </dl>
|
|
|
146 |
*/
|
|
|
147 |
Y.Intl.publish("intl:langChange", {emitFacade:true});
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
}, '3.18.1', {"requires": ["intl-base", "event-custom"]});
|