intl.js revision 30551f406370f7c8bf461a833205311de720886d
YUI.add('intl', function(Y) {
var _mods = {},
ROOT_LANG = "yuiRootLang",
ACTIVE_LANG = "yuiActiveLang";
/**
* The intl-lang sub-module adds the ability to store and retrieve multiple sets of language strings on the client.
*
* @module intl
* @submodule intl-lang
*/
/**
* The Intl utility provides a central location for managing language specific sets of strings and formatting patterns.
*
* @class Intl
* @static
*/
Y.mix(Y.namespace("Intl"), {
/**
* @method _mod
* @private
*
* @param {String} module The name of the module
* @return {Object} The hash of localized strings for the module, keyed by BCP language tag
*/
_mod : function(module) {
if (!_mods[module]) {
_mods[module] = {};
}
return _mods[module];
},
/**
* Sets the active language for the given module.
*
* Returns false on failure, which would happen if the language had not been registered through the <a href="#method_add">add()</a> method.
*
* @method setLang
* @private
*
* @param {String} module The module name.
* @param {String} lang The BCP 47 language tag.
*/
_setLang : function(module, lang) {
var langs = this._mod(module),
currLang = langs[ACTIVE_LANG],
exists = !!langs[lang];
if (exists) {
langs[ACTIVE_LANG] = lang;
this.fire("intl:langChange", {module: module, prevVal: currLang, newVal: (lang === ROOT_LANG) ? "" : lang});
}
return exists;
},
/**
* Get the active language for the given module.
*
* @method getLang
*
* @param {String} module The module name.
* @param {String} lang The BCP 47 language tag.
*
* @return {String} The current BCP 47 language tag.
*/
getLang : function(module) {
var lang = this._mod(module)[ACTIVE_LANG];
return (lang === ROOT_LANG) ? "" : lang;
},
/**
* Register a set of strings for the given module and language
*
* @method add
*
* @param {String} module The module name.
* @param {String} lang The BCP 47 language tag.
* @param {Object} strings The hash of strings.
*/
add : function(module, lang, strings) {
lang = lang || ROOT_LANG;
this._mod(module)[lang] = strings;
this._setLang(module, lang);
},
/**
* Get the module strings for the given BCP language tag.
*
* @method get
*
* @param {String} module The module name.
* @param {String} key Optional. If not provided, returns a shallow cloned hash of all strings (to protect the originals).
* @param {String} lang Optional. The BCP 47 langauge tag.
* @return String | Object
*/
get : function(module, key, lang) {
var mod = this._mod(module),
strs;
lang = lang || mod[ACTIVE_LANG];
strs = mod[lang] || {};
return (key) ? strs[key] : Y.merge(strs);
},
/**
* Obtains the list of languages for which resource bundles are available for a given module, based on the module
* meta-data (part of loader). If loader is not on the page, returns an empty array.
*
* @param {String} module The name of the module
* @return {Array} The list of languages available.
*/
getAvailableLangs : function(module) {
var availLangs = [],
allLangs = Y.Env && Y.Env.lang,
lang;
// Y.Env.lang[lang][m.name]
if(allLangs) {
for (lang in allLangs) {
if (lang && allLangs.hasOwnProperty(lang)) {
if (allLangs[lang][module]) {
availLangs[availLangs.length] = lang;
}
}
}
}
return availLangs;
}
});
Y.augment(Y.Intl, Y.EventTarget);
Y.Intl.publish("intl:langChange", {emitFacade:true});
}, '@VERSION@' ,{requires:['event-custom']});