https://www.mediawiki.org/wiki/Special:Code/MediaWiki/112479
Revision: 112479
Author: robin
Date: 2012-02-27 11:59:24 +0000 (Mon, 27 Feb 2012)
Log Message:
-----------
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
Modified Paths:
--------------
trunk/extensions/Translate/TranslateUtils.php
trunk/phase3/includes/Preferences.php
trunk/phase3/includes/Xml.php
trunk/phase3/includes/parser/CoreParserFunctions.php
trunk/phase3/languages/Language.php
Modified: trunk/extensions/Translate/TranslateUtils.php
===================================================================
--- trunk/extensions/Translate/TranslateUtils.php 2012-02-27 10:51:57 UTC
(rev 112478)
+++ trunk/extensions/Translate/TranslateUtils.php 2012-02-27 11:59:24 UTC
(rev 112479)
@@ -187,8 +187,8 @@
* @return array
*/
public static function getLanguageNames( /*string */ $code ) {
- if ( is_callable( array( 'Language',
'getTranslatedLanguageNames' ) ) ) {
- return Language::getTranslatedLanguageNames( $code );
+ if ( is_callable( array( 'Language', 'fetchLanguageNames' ) ) )
{
+ return Language::fetchLanguageNames( $code, 'mw' ); //
since 1.20
} elseif ( is_callable( array( 'LanguageNames', 'getNames' ) )
) {
return LanguageNames::getNames( $code,
LanguageNames::FALLBACK_NORMAL,
Modified: trunk/phase3/includes/Preferences.php
===================================================================
--- trunk/phase3/includes/Preferences.php 2012-02-27 10:51:57 UTC (rev
112478)
+++ trunk/phase3/includes/Preferences.php 2012-02-27 11:59:24 UTC (rev
112479)
@@ -252,7 +252,7 @@
}
// Language
- $languages = Language::getLanguageNames( false );
+ $languages = Language::fetchLanguageNames( null, 'mw' );
if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
$languages[$wgLanguageCode] = $wgLanguageCode;
}
Modified: trunk/phase3/includes/Xml.php
===================================================================
--- trunk/phase3/includes/Xml.php 2012-02-27 10:51:57 UTC (rev 112478)
+++ trunk/phase3/includes/Xml.php 2012-02-27 11:59:24 UTC (rev 112479)
@@ -190,29 +190,13 @@
*
* @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...
Modified: trunk/phase3/includes/parser/CoreParserFunctions.php
===================================================================
--- trunk/phase3/includes/parser/CoreParserFunctions.php 2012-02-27
10:51:57 UTC (rev 112478)
+++ trunk/phase3/includes/parser/CoreParserFunctions.php 2012-02-27
11:59:24 UTC (rev 112479)
@@ -622,21 +622,14 @@
/**
* 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 );
}
Modified: trunk/phase3/languages/Language.php
===================================================================
--- trunk/phase3/languages/Language.php 2012-02-27 10:51:57 UTC (rev 112478)
+++ trunk/phase3/languages/Language.php 2012-02-27 11:59:24 UTC (rev 112479)
@@ -659,29 +659,10 @@
* @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,19 +672,86 @@
* @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 ) );
}
- return $names;
+ $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];
+ }
+
+ 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];
+ }
+
+ /**
* Get a message from the MediaWiki namespace.
*
* @param $msg String: message name
@@ -718,13 +766,10 @@
* 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 );
}
/**
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs