diff --git a/modules/oojs-ui/OO.ui.js b/modules/oojs-ui/OO.ui.js index 4bba1844e3..cd1eee463f 100644 --- a/modules/oojs-ui/OO.ui.js +++ b/modules/oojs-ui/OO.ui.js @@ -16,8 +16,9 @@ OO.ui = {}; OO.ui.bind = $.proxy; /** - * Get the user's language and any fallback languages. These language codes are used by - * OO.ui.IconedElement to select localized icons in the user's language. + * Get the user's language and any fallback languages. + * + * These language codes are used to localize user interface elements in the user's language. * * In environments that provide a localization system, this function should be overridden to * return the user's language(s). The default implementation returns English (en) only. @@ -28,6 +29,41 @@ OO.ui.getUserLanguages = function () { return [ 'en' ]; }; +/** + * Get a value in an object keyed by language code. + * + * @param {Object.} obj Object keyed by language code + * @param {string|null} [lang] Language code, if omitted or null defaults to any user language + * @param {string} [fallback] Fallback code, used if no matching language can be found + * @returns {Mixed} Local value + */ +OO.ui.getLocalValue = function ( obj, lang, fallback ) { + var i, len, langs; + + // Requested language + if ( obj[lang] ) { + return obj[lang]; + } + // Known user language + langs = OO.ui.getUserLanguages(); + for ( i = 0, len = langs.length; i < len; i++ ) { + lang = langs[i]; + if ( obj[lang] ) { + return obj[lang]; + } + } + // Fallback language + if ( obj[fallback] ) { + return obj[fallback]; + } + // First existing language + for ( lang in obj ) { + return obj[lang]; + } + + return undefined; +}; + ( function () { /** diff --git a/modules/oojs-ui/elements/OO.ui.IconedElement.js b/modules/oojs-ui/elements/OO.ui.IconedElement.js index 09612f00e8..5771d7d4e5 100644 --- a/modules/oojs-ui/elements/OO.ui.IconedElement.js +++ b/modules/oojs-ui/elements/OO.ui.IconedElement.js @@ -40,21 +40,7 @@ OO.ui.IconedElement = function OoUiIconedElement( $icon, config ) { * @chainable */ OO.ui.IconedElement.prototype.setIcon = function ( value ) { - var i, len, icon, lang, - langs = OO.ui.getUserLanguages(); - - if ( OO.isPlainObject( value ) ) { - icon = value['default']; - for ( i = 0, len = langs.length; i < len; i++ ) { - lang = langs[i]; - if ( value[lang] ) { - icon = value[lang]; - break; - } - } - } else { - icon = value; - } + var icon = OO.isPlainObject( value ) ? OO.ui.getLocalValue( value, null, 'default' ) : value; if ( this.icon ) { this.$icon.removeClass( 'oo-ui-icon-' + this.icon ); diff --git a/modules/ve-mw/dm/models/ve.dm.MWTemplateSpecModel.js b/modules/ve-mw/dm/models/ve.dm.MWTemplateSpecModel.js index 13b6be5807..934052a8d9 100644 --- a/modules/ve-mw/dm/models/ve.dm.MWTemplateSpecModel.js +++ b/modules/ve-mw/dm/models/ve.dm.MWTemplateSpecModel.js @@ -141,10 +141,12 @@ ve.dm.MWTemplateSpecModel.prototype.getLabel = function () { * Get template description. * * @method + * @param {string} [lang] Language to get description in * @returns {string|null} Template description or null if not available */ -ve.dm.MWTemplateSpecModel.prototype.getDescription = function () { - return this.description; +ve.dm.MWTemplateSpecModel.prototype.getDescription = function ( lang ) { + var value = this.description; + return ve.isPlainObject( value ) ? OO.ui.getLocalValue( value, lang ) : value; }; /** @@ -176,10 +178,12 @@ ve.dm.MWTemplateSpecModel.prototype.isParameterAlias = function ( name ) { * * @method * @param {string} name Parameter name + * @param {string} [lang] Language to get label in * @returns {string} Parameter label */ -ve.dm.MWTemplateSpecModel.prototype.getParameterLabel = function ( name ) { - return this.params[name].label || name; +ve.dm.MWTemplateSpecModel.prototype.getParameterLabel = function ( name, lang ) { + var value = this.params[name].label; + return ve.isPlainObject( value ) ? OO.ui.getLocalValue( value, lang ) : value; }; /** @@ -187,10 +191,12 @@ ve.dm.MWTemplateSpecModel.prototype.getParameterLabel = function ( name ) { * * @method * @param {string} name Parameter name + * @param {string} [lang] Language to get description * @returns {string|null} Parameter description */ -ve.dm.MWTemplateSpecModel.prototype.getParameterDescription = function ( name ) { - return this.params[name].description; +ve.dm.MWTemplateSpecModel.prototype.getParameterDescription = function ( name, lang ) { + var value = this.params[name].description; + return ve.isPlainObject( value ) ? OO.ui.getLocalValue( value, lang ) : value; }; /**