view/resources/wikibase/wikibase.getLanguageNameByCode.js
/**
* Provide best-effort language names from different sources.
*
* @license GPL-2.0-or-later
*/
( function ( wb ) {
'use strict';
// Implementation note: LanguageNameLookup contains similar functionality in PHP.
// synthetic file generated by PHP (currently ViewHooks::onResourceLoaderRegisterModules())
var wikibaseLanguageNames = require( './languageNames.json' );
/**
* Get the autonym (language name in that language) of the given language,
* if ULS is installed and knows an autonym for the language.
*
* @param {string} langCode
* @return {string|null}
*/
var getUlsAutonym = function ( langCode ) {
if ( $.uls && $.uls.data.languages ) {
var languages = $.uls.data.languages;
if ( Object.prototype.hasOwnProperty.call( languages, langCode ) && languages[ langCode ][ 2 ] ) {
return languages[ langCode ][ 2 ];
}
}
return null;
};
/**
* Get the language name (in the user language) of the given language,
* if ULS is installed and knows a language name for it (possibly with language fallbacks).
*
* @param {string} langCode
* @return {string|null}
*/
var getUlsLanguageName = function ( langCode ) {
if ( $.fn.uls && $.fn.uls.defaults.languages ) {
return $.fn.uls.defaults.languages[ langCode ] || null;
}
return null;
};
/**
* Get the language name (in the user language) of the given language,
* from the data sent by the Wikibase server-side code.
*
* In theory, we could get all the language names this way,
* but because ULS already sends some language names
* and we want to avoid sending the same data twice,
* the server-side code removes language names that are included in the ULS data.
* Thus, if ULS is installed, this function only provides language names
* for languages not supported by ULS.
*
* @param {string} langCode
* @return {string|null}
*/
var getWikibaseLanguageName = function ( langCode ) {
return wikibaseLanguageNames[ langCode ] || null;
};
/**
* Returns the name of a language in the UI language, if available.
* May fall back to the languageās autonym or even language code.
*
* In a terms context, use {@link wikibase.getLanguageNameByCodeForTerms} instead.
*
* @param {string} langCode
* @return {string}
*/
wb.getLanguageNameByCode = function ( langCode ) {
return getWikibaseLanguageName( langCode ) ||
getUlsLanguageName( langCode ) ||
getUlsAutonym( langCode ) ||
langCode;
};
/**
* Returns the name of a language for a terms context,
* i.e. when the language code is used for labels, descriptions or aliases.
* For other contexts, see {@link wikibase.getLanguageNameByCode}.
*
* The 'mul' language code has a special meaning for terms,
* and gets a distinct name in this context to communicate this meaning.
* For other language codes, this is equivalent to {@link wikibase.getLanguageNameByCode}.
*
* @param {string} langCode
* @returns {string}
*/
wb.getLanguageNameByCodeForTerms = function ( langCode ) {
if ( langCode === 'mul' ) {
return mw.msg( 'wikibase-language-name-for-terms-mul' );
}
return wb.getLanguageNameByCode( langCode );
};
}( wikibase ) );