From 44d2c73bf5b1049bbac75c1b0c87a2bb0b6a3cef Mon Sep 17 00:00:00 2001 From: Robin Pepermans Date: Mon, 27 Feb 2012 11:59:24 +0000 Subject: [PATCH] Combine getLanguageNames and getTranslatedLanguageNames into one function which is much easier and more logical to use. Does not contain any actual changes yet in what the function does. * Reduces the overly long code in r107002, and reduces code for {{#language:}} * Fixes the language list in Special:Translate which contained languages that gave "invalid code" when selecting --- includes/Preferences.php | 2 +- includes/Xml.php | 22 +---- includes/parser/CoreParserFunctions.php | 17 ++-- languages/Language.php | 105 +++++++++++++++++------- 4 files changed, 84 insertions(+), 62 deletions(-) diff --git a/includes/Preferences.php b/includes/Preferences.php index 1baf453545..a003c9f824 100644 --- a/includes/Preferences.php +++ b/includes/Preferences.php @@ -252,7 +252,7 @@ class Preferences { } // Language - $languages = Language::getLanguageNames( false ); + $languages = Language::fetchLanguageNames( null, 'mw' ); if ( !array_key_exists( $wgLanguageCode, $languages ) ) { $languages[$wgLanguageCode] = $wgLanguageCode; } diff --git a/includes/Xml.php b/includes/Xml.php index 8135a70d7a..2e882ed3ff 100644 --- a/includes/Xml.php +++ b/includes/Xml.php @@ -190,29 +190,13 @@ class Xml { * * @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... diff --git a/includes/parser/CoreParserFunctions.php b/includes/parser/CoreParserFunctions.php index 9c39e8ad43..a07077347c 100644 --- a/includes/parser/CoreParserFunctions.php +++ b/includes/parser/CoreParserFunctions.php @@ -622,21 +622,14 @@ class CoreParserFunctions { /** * 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 ); } diff --git a/languages/Language.php b/languages/Language.php index 40dcda3008..6e968b03cd 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -659,29 +659,10 @@ class Language { * @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' ); } /** @@ -691,16 +672,83 @@ class Language { * @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]; } /** @@ -718,13 +766,10 @@ class Language { * 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 ); } /** -- 2.20.1