Merge "Make generic localization utility for lang code keyed objects"

This commit is contained in:
jenkins-bot 2013-11-13 22:37:02 +00:00 committed by Gerrit Code Review
commit 3b0c87f5e3
3 changed files with 51 additions and 23 deletions

View file

@ -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.<string,Mixed>} 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 () {
/**

View file

@ -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 );

View file

@ -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;
};
/**