8698ba587fa8ee1d6aa74483b9e5efe8c0f4b701Satyen DesaiYUI.add('intl', function(Y) {
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desaivar _mods = {},
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai
0b5dc5582c28b4b1f31e384c65f8b28ded11d277Satyen Desai ROOT_LANG = "yuiRootLang",
54c01833b669ee8dbd4b309a4be2faa58e28ab0dAdam Moore ACTIVE_LANG = "yuiActiveLang",
54c01833b669ee8dbd4b309a4be2faa58e28ab0dAdam Moore NONE = [];
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai
54c01833b669ee8dbd4b309a4be2faa58e28ab0dAdam Moore/**
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * Provides utilities to support the management of localized resources (strings and formatting patterns).
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai *
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai * @module intl
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai */
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai
54c01833b669ee8dbd4b309a4be2faa58e28ab0dAdam Moore/**
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * The Intl utility provides a central location for managing sets of localized resources (strings and formatting patterns).
0b5dc5582c28b4b1f31e384c65f8b28ded11d277Satyen Desai *
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai * @class Intl
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * @uses EventTarget
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai * @static
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai */
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen DesaiY.mix(Y.namespace("Intl"), {
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai /**
54c01833b669ee8dbd4b309a4be2faa58e28ab0dAdam Moore * Private method to retrieve the language hash for a given module.
54c01833b669ee8dbd4b309a4be2faa58e28ab0dAdam Moore *
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai * @method _mod
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai * @private
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai *
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai * @param {String} module The name of the module
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * @return {Object} The hash of localized resources for the module, keyed by BCP language tag
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai */
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai _mod : function(module) {
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai if (!_mods[module]) {
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai _mods[module] = {};
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai }
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai return _mods[module];
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai },
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai /**
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai * Sets the active language for the given module.
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai *
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai * Returns false on failure, which would happen if the language had not been registered through the <a href="#method_add">add()</a> method.
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai *
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai * @method setLang
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai *
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai * @param {String} module The module name.
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai * @param {String} lang The BCP 47 language tag.
54c01833b669ee8dbd4b309a4be2faa58e28ab0dAdam Moore * @return boolean true if successful, false if not.
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai */
998081668c864c09fb71c747abae48a38df0f79bSatyen Desai setLang : function(module, lang) {
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai var langs = this._mod(module),
0b5dc5582c28b4b1f31e384c65f8b28ded11d277Satyen Desai currLang = langs[ACTIVE_LANG],
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai exists = !!langs[lang];
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai
998081668c864c09fb71c747abae48a38df0f79bSatyen Desai if (exists && lang !== currLang) {
0b5dc5582c28b4b1f31e384c65f8b28ded11d277Satyen Desai langs[ACTIVE_LANG] = lang;
30551f406370f7c8bf461a833205311de720886dSatyen Desai this.fire("intl:langChange", {module: module, prevVal: currLang, newVal: (lang === ROOT_LANG) ? "" : lang});
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai }
0b5dc5582c28b4b1f31e384c65f8b28ded11d277Satyen Desai
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai return exists;
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai },
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai /**
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * Get the currently active language for the given module.
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai *
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai * @method getLang
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai *
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai * @param {String} module The module name.
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * @return {String} The BCP 47 language tag.
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai */
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai getLang : function(module) {
54c01833b669ee8dbd4b309a4be2faa58e28ab0dAdam Moore var lang = this._mod(module)[ACTIVE_LANG];
30551f406370f7c8bf461a833205311de720886dSatyen Desai return (lang === ROOT_LANG) ? "" : lang;
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai },
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai /**
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * Register a hash of localized resources for the given module and language
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai *
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai * @method add
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai *
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai * @param {String} module The module name.
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai * @param {String} lang The BCP 47 language tag.
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * @param {Object} strings The hash of localized values, keyed by the string name.
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai */
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai add : function(module, lang, strings) {
0b5dc5582c28b4b1f31e384c65f8b28ded11d277Satyen Desai lang = lang || ROOT_LANG;
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai this._mod(module)[lang] = strings;
998081668c864c09fb71c747abae48a38df0f79bSatyen Desai this.setLang(module, lang);
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai },
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai /**
b07c442afe94f55ef7b93a69046dd8291d1bbaacSatyen Desai * Gets the module's localized resources for the currently active language (as provided by the <a href="#method_getLang">getLang</a> method).
b07c442afe94f55ef7b93a69046dd8291d1bbaacSatyen Desai * <p>
54c01833b669ee8dbd4b309a4be2faa58e28ab0dAdam Moore * Optionally, the localized resources for alternate languages which have been added to Intl (see the <a href="#method_add">add</a> method) can
54c01833b669ee8dbd4b309a4be2faa58e28ab0dAdam Moore * be retrieved by providing the BCP 47 language tag as the lang parameter.
b07c442afe94f55ef7b93a69046dd8291d1bbaacSatyen Desai * </p>
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai * @method get
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai *
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai * @param {String} module The module name.
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * @param {String} key Optional. A single resource key. If not provided, returns a copy (shallow clone) of all resources.
85786151283612d4c36a6189954fe75894b4376fSatyen Desai * @param {String} lang Optional. The BCP 47 language tag. If not provided, the module's currently active language is used.
b07c442afe94f55ef7b93a69046dd8291d1bbaacSatyen Desai * @return String | Object A copy of the module's localized resources, or a single value if key is provided.
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai */
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai get : function(module, key, lang) {
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai var mod = this._mod(module),
d64d8e4c60baa647c81e7917fdfd3c86ca023cb8Satyen Desai strs;
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai
0b5dc5582c28b4b1f31e384c65f8b28ded11d277Satyen Desai lang = lang || mod[ACTIVE_LANG];
d64d8e4c60baa647c81e7917fdfd3c86ca023cb8Satyen Desai strs = mod[lang] || {};
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai
d64d8e4c60baa647c81e7917fdfd3c86ca023cb8Satyen Desai return (key) ? strs[key] : Y.merge(strs);
8698ba587fa8ee1d6aa74483b9e5efe8c0f4b701Satyen Desai },
8698ba587fa8ee1d6aa74483b9e5efe8c0f4b701Satyen Desai
8698ba587fa8ee1d6aa74483b9e5efe8c0f4b701Satyen Desai /**
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * Gets the list of languages for which localized resources are available for a given module, based on the module
8698ba587fa8ee1d6aa74483b9e5efe8c0f4b701Satyen Desai * meta-data (part of loader). If loader is not on the page, returns an empty array.
8698ba587fa8ee1d6aa74483b9e5efe8c0f4b701Satyen Desai *
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * @method getAvailableLangs
8698ba587fa8ee1d6aa74483b9e5efe8c0f4b701Satyen Desai * @param {String} module The name of the module
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * @return {Array} The array of languages available.
8698ba587fa8ee1d6aa74483b9e5efe8c0f4b701Satyen Desai */
8698ba587fa8ee1d6aa74483b9e5efe8c0f4b701Satyen Desai getAvailableLangs : function(module) {
54c01833b669ee8dbd4b309a4be2faa58e28ab0dAdam Moore var loader = Y.Env._loader,
54c01833b669ee8dbd4b309a4be2faa58e28ab0dAdam Moore mod = loader && loader.moduleInfo[module],
54c01833b669ee8dbd4b309a4be2faa58e28ab0dAdam Moore langs = mod && mod.lang;
54c01833b669ee8dbd4b309a4be2faa58e28ab0dAdam Moore return (langs) ? langs.concat() : NONE;
8698ba587fa8ee1d6aa74483b9e5efe8c0f4b701Satyen Desai
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai }
628b3b052ecff4d1843c7e919951413fbc03e79fSatyen Desai});
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen DesaiY.augment(Y.Intl, Y.EventTarget);
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai/**
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * Notification event to indicate when the lang for a module has changed. There is no default behavior associated with this event,
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * so the on and after moments are equivalent.
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai *
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * @event intl:langChange
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * @param {EventFacade} e The event facade
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * <p>The event facade contains:</p>
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * <dl>
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * <dt>module</dt><dd>The name of the module for which the language changed</dd>
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * <dt>newVal</dt><dd>The new language tag</dd>
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * <dt>prevVal</dt><dd>The current language tag</dd>
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai * </dl>
7251b45ed93bd168e55d3ccd3b45a7f6e1c378c1Satyen Desai */
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen DesaiY.Intl.publish("intl:langChange", {emitFacade:true});
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai
d894ed8f7003b6c2ec19d5fb3969b6c45f0d7147Satyen Desai
cc22ee4dba50c26571d1e3fca1ff042e6671b7e1Dav Glass}, '@VERSION@' ,{requires:['event-custom', 'intl-base']});