*
* @param string $selected The language code of the selected language
* @param boolean $customisedOnly If true only languages which have some content are listed
- * @param string $language The ISO code of the language to display the select list in (optional)
+ * @param string $inLanguage The ISO code of the language to display the select list in (optional)
* @return array containing 2 items: label HTML and select list HTML
*/
- public static function languageSelector( $selected, $customisedOnly = true, $language = null ) {
+ public static function languageSelector( $selected, $customisedOnly = true, $inLanguage = null ) {
global $wgLanguageCode;
- // TODO: This should be replaced with a hook or something, from r107002
- // If a specific language was requested and CLDR is installed, use it
- if ( $language && is_callable( array( 'LanguageNames', 'getNames' ) ) ) {
- if ( $customisedOnly ) {
- $listType = LanguageNames::LIST_MW_SUPPORTED; // Only pull names that have localisation in MediaWiki
- } else {
- $listType = LanguageNames::LIST_MW; // Pull all languages that are in Names.php
- }
- // Retrieve the list of languages in the requested language (via CLDR)
- $languages = LanguageNames::getNames(
- $language, // Code of the requested language
- LanguageNames::FALLBACK_NORMAL, // Use fallback chain
- $listType
- );
- } else {
- $languages = Language::getLanguageNames( $customisedOnly );
- }
+ $languages = Language::fetchLanguageNames( $inLanguage, $customisedOnly ? 'mwfile' : 'mw' );
// Make sure the site language is in the list; a custom language code might not have a
// defined name...
/**
* Gives language names.
* @param $parser Parser
- * @param $code String Language code
- * @param $language String Language code
+ * @param $code String Language code (of which to get name)
+ * @param $inLnguage String Language code (in which to get name)
* @return String
*/
- static function language( $parser, $code = '', $language = '' ) {
- global $wgContLang;
+ static function language( $parser, $code = '', $inLanguage = '' ) {
$code = strtolower( $code );
- $language = strtolower( $language );
-
- if ( $language !== '' ) {
- $names = Language::getTranslatedLanguageNames( $language );
- return isset( $names[$code] ) ? $names[$code] : wfBCP47( $code );
- }
-
- $lang = $wgContLang->getLanguageName( $code );
+ $inLanguage = strtolower( $inLanguage );
+ $lang = Language::fetchLanguageName( $code, $inLanguage );
return $lang !== '' ? $lang : wfBCP47( $code );
}
* @param $customisedOnly bool
*
* @return array
+ * @deprecated in 1.20, use fetchLanguageNames()
*/
public static function getLanguageNames( $customisedOnly = false ) {
- global $wgExtraLanguageNames;
- static $coreLanguageNames;
-
- if ( $coreLanguageNames === null ) {
- include( MWInit::compiledPath( 'languages/Names.php' ) );
- }
-
- $allNames = $wgExtraLanguageNames + $coreLanguageNames;
- if ( !$customisedOnly ) {
- return $allNames;
- }
-
- $names = array();
- // We do this using a foreach over the codes instead of a directory
- // loop so that messages files in extensions will work correctly.
- foreach ( $allNames as $code => $value ) {
- if ( is_readable( self::getMessagesFileName( $code ) ) ) {
- $names[$code] = $allNames[$code];
- }
- }
- return $names;
+ return self::fetchLanguageNames( null, $customisedOnly ? 'mwfile' : 'mw' );
}
/**
* @param $code String Language code.
* @return Array language code => language name
* @since 1.18.0
+ * @deprecated in 1.20, use fetchLanguageNames()
*/
public static function getTranslatedLanguageNames( $code ) {
+ return self::fetchLanguageNames( $code, 'all' );
+ }
+
+
+ /*
+ * Get an array of language names, indexed by code.
+ * @param $inLanguage null|string: Code of language in which to return the names
+ * Use null for autonyms (native names)
+ * @param $include string:
+ * 'all' all available languages
+ * 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames
+ * 'mwfile' only if the language is in 'mw' *and* has a message file
+ * @return array|false: language code => language name, false if $include is wrong
+ */
+ public static function fetchLanguageNames( $inLanguage = null, $include = 'all' ) {
+ global $wgExtraLanguageNames;
+ static $coreLanguageNames;
+
+ if ( $coreLanguageNames === null ) {
+ include( MWInit::compiledPath( 'languages/Names.php' ) );
+ }
+
$names = array();
- wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $code ) );
- foreach ( self::getLanguageNames() as $code => $name ) {
- if ( !isset( $names[$code] ) ) $names[$code] = $name;
+ if( $inLanguage ) {
+ # TODO: also include when $inLanguage is null, when this code is more efficient
+ wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) );
+ }
+
+ $mwNames = $wgExtraLanguageNames + $coreLanguageNames;
+ foreach ( $mwNames as $mwCode => $mwName ) {
+ # - Prefer own MediaWiki native name when not using the hook
+ # TODO: prefer it always to make it consistent, but casing is different in CLDR
+ # - For other names just add if not added through the hook
+ if ( ( $mwCode === $inLanguage && !$inLanguage ) || !isset( $names[$mwCode] ) ) {
+ $names[$mwCode] = $mwName;
+ }
+ }
+
+ if ( $include === 'all' ) {
+ return $names;
+ }
+
+ $returnMw = array();
+ $coreCodes = array_keys( $mwNames );
+ foreach( $coreCodes as $coreCode ) {
+ $returnMw[$coreCode] = $names[$coreCode];
}
- return $names;
+ if( $include === 'mw' ) {
+ return $returnMw;
+ } elseif( $include === 'mwfile' ) {
+ $namesMwFile = array();
+ # We do this using a foreach over the codes instead of a directory
+ # loop so that messages files in extensions will work correctly.
+ foreach ( $returnMw as $code => $value ) {
+ if ( is_readable( self::getMessagesFileName( $code ) ) ) {
+ $namesMwFile[$code] = $names[$code];
+ }
+ }
+ return $namesMwFile;
+ }
+ return false;
+ }
+
+ /**
+ * @param $code string: The code of the language for which to get the name
+ * @param $inLanguage null|string: Code of language in which to return the name (null for autonyms)
+ * @return string: Language name or empty
+ * @since 1.20
+ */
+ public static function fetchLanguageName( $code, $inLanguage = null ) {
+ $array = self::fetchLanguageNames( $inLanguage, 'all' );
+ return !array_key_exists( $code, $array ) ? '' : $array[$code];
}
/**
* Only if defined in MediaWiki, no other data like CLDR.
* @param $code string
* @return string
+ * @deprecated in 1.20, use fetchLanguageName()
*/
function getLanguageName( $code ) {
- $names = self::getLanguageNames();
- if ( !array_key_exists( $code, $names ) ) {
- return '';
- }
- return $names[$code];
+ return self::fetchLanguageName( $code );
}
/**