All files / mobile.startup LanguageInfo.js

20% Statements 4/20
0% Branches 0/4
0% Functions 0/8
21.05% Lines 4/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 651x 1x                       1x                                                                                                   1x  
var util = require( './util.js' ),
	actionParams = require( './actionParams' );
 
/**
 * API for providing language data.
 *
 * @class LanguageInfo
 * @param {mw.Api} api
 */
function LanguageInfo( api ) {
	this.api = api;
}
 
LanguageInfo.prototype = {
 
	/**
	 * Get languageinfo API data from the local wiki, and transform it into a
	 * format usable by LanguageSearcher.
	 *
	 * @memberof LanguageInfo
	 * @instance
	 * @returns {jQuery.Deferred}
	 */
	getLanguages() {
		return this.api.get( actionParams( {
			meta: 'languageinfo',
			liprop: 'code|autonym|name|bcp47'
		} ) ).then( ( resp ) => {
			const filteredLanguages = [];
			// Filter out legacy languages and require an autonym.
			// If the bcp47 (https://w.wiki/Y7A) does not match the language
			// code, that is in an indication that the language code is outdated
			// and should not be used.
			Object.keys( resp.query.languageinfo ).forEach( ( key ) => {
				const language = resp.query.languageinfo[key];
				if ( ( language.code.toLowerCase() === language.bcp47.toLowerCase() ) &&
					language.autonym ) {
					filteredLanguages.push( language );
				}
			} );
			return filteredLanguages;
		}, () => util.Deferred().reject() ).then( ( filteredLanguages ) => ( {
			languages: filteredLanguages.map( ( data ) => {
				data.url = '#';
				data.lang = data.code;
				data.langname = data.name;
				// FIXME: This isn't a "title" in the sense of a MediaWiki
				// Title, and it is rendered as a subheader in the list
				// item, so a different name would be wiser, both here and
				// in LanguageSearcher's template. Also it would arguably
				// be more intuitive for the language name (localized) to
				// appear as the main emphasized element of each language
				// list element; but instead the autonym has that role.
				// A more thorough refactoring of LanguageSearcher to allow
				// for generic header/subheader elements is left for a
				// follow-up.
				data.title = data.name;
				return data;
			} )
		} ), () => util.Deferred().reject() );
	}
};
 
module.exports = LanguageInfo;