r112479 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112478‎ | r112479 | r112480 >
Date:11:59, 27 February 2012
Author:robin
Status:ok (Comments)
Tags:i18nreview 
Comment:
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 (modified) (history)
  • /trunk/phase3/includes/Preferences.php (modified) (history)
  • /trunk/phase3/includes/Xml.php (modified) (history)
  • /trunk/phase3/includes/parser/CoreParserFunctions.php (modified) (history)
  • /trunk/phase3/languages/Language.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Xml.php
@@ -190,29 +190,13 @@
191191 *
192192 * @param string $selected The language code of the selected language
193193 * @param boolean $customisedOnly If true only languages which have some content are listed
194 - * @param string $language The ISO code of the language to display the select list in (optional)
 194+ * @param string $inLanguage The ISO code of the language to display the select list in (optional)
195195 * @return array containing 2 items: label HTML and select list HTML
196196 */
197 - public static function languageSelector( $selected, $customisedOnly = true, $language = null ) {
 197+ public static function languageSelector( $selected, $customisedOnly = true, $inLanguage = null ) {
198198 global $wgLanguageCode;
199199
200 - // TODO: This should be replaced with a hook or something, from r107002
201 - // If a specific language was requested and CLDR is installed, use it
202 - if ( $language && is_callable( array( 'LanguageNames', 'getNames' ) ) ) {
203 - if ( $customisedOnly ) {
204 - $listType = LanguageNames::LIST_MW_SUPPORTED; // Only pull names that have localisation in MediaWiki
205 - } else {
206 - $listType = LanguageNames::LIST_MW; // Pull all languages that are in Names.php
207 - }
208 - // Retrieve the list of languages in the requested language (via CLDR)
209 - $languages = LanguageNames::getNames(
210 - $language, // Code of the requested language
211 - LanguageNames::FALLBACK_NORMAL, // Use fallback chain
212 - $listType
213 - );
214 - } else {
215 - $languages = Language::getLanguageNames( $customisedOnly );
216 - }
 200+ $languages = Language::fetchLanguageNames( $inLanguage, $customisedOnly ? 'mwfile' : 'mw' );
217201
218202 // Make sure the site language is in the list; a custom language code might not have a
219203 // defined name...
Index: trunk/phase3/includes/parser/CoreParserFunctions.php
@@ -622,21 +622,14 @@
623623 /**
624624 * Gives language names.
625625 * @param $parser Parser
626 - * @param $code String Language code
627 - * @param $language String Language code
 626+ * @param $code String Language code (of which to get name)
 627+ * @param $inLnguage String Language code (in which to get name)
628628 * @return String
629629 */
630 - static function language( $parser, $code = '', $language = '' ) {
631 - global $wgContLang;
 630+ static function language( $parser, $code = '', $inLanguage = '' ) {
632631 $code = strtolower( $code );
633 - $language = strtolower( $language );
634 -
635 - if ( $language !== '' ) {
636 - $names = Language::getTranslatedLanguageNames( $language );
637 - return isset( $names[$code] ) ? $names[$code] : wfBCP47( $code );
638 - }
639 -
640 - $lang = $wgContLang->getLanguageName( $code );
 632+ $inLanguage = strtolower( $inLanguage );
 633+ $lang = Language::fetchLanguageName( $code, $inLanguage );
641634 return $lang !== '' ? $lang : wfBCP47( $code );
642635 }
643636
Index: trunk/phase3/includes/Preferences.php
@@ -252,7 +252,7 @@
253253 }
254254
255255 // Language
256 - $languages = Language::getLanguageNames( false );
 256+ $languages = Language::fetchLanguageNames( null, 'mw' );
257257 if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
258258 $languages[$wgLanguageCode] = $wgLanguageCode;
259259 }
Index: trunk/phase3/languages/Language.php
@@ -659,29 +659,10 @@
660660 * @param $customisedOnly bool
661661 *
662662 * @return array
 663+ * @deprecated in 1.20, use fetchLanguageNames()
663664 */
664665 public static function getLanguageNames( $customisedOnly = false ) {
665 - global $wgExtraLanguageNames;
666 - static $coreLanguageNames;
667 -
668 - if ( $coreLanguageNames === null ) {
669 - include( MWInit::compiledPath( 'languages/Names.php' ) );
670 - }
671 -
672 - $allNames = $wgExtraLanguageNames + $coreLanguageNames;
673 - if ( !$customisedOnly ) {
674 - return $allNames;
675 - }
676 -
677 - $names = array();
678 - // We do this using a foreach over the codes instead of a directory
679 - // loop so that messages files in extensions will work correctly.
680 - foreach ( $allNames as $code => $value ) {
681 - if ( is_readable( self::getMessagesFileName( $code ) ) ) {
682 - $names[$code] = $allNames[$code];
683 - }
684 - }
685 - return $names;
 666+ return self::fetchLanguageNames( null, $customisedOnly ? 'mwfile' : 'mw' );
686667 }
687668
688669 /**
@@ -691,19 +672,86 @@
692673 * @param $code String Language code.
693674 * @return Array language code => language name
694675 * @since 1.18.0
 676+ * @deprecated in 1.20, use fetchLanguageNames()
695677 */
696678 public static function getTranslatedLanguageNames( $code ) {
 679+ return self::fetchLanguageNames( $code, 'all' );
 680+ }
 681+
 682+
 683+ /*
 684+ * Get an array of language names, indexed by code.
 685+ * @param $inLanguage null|string: Code of language in which to return the names
 686+ * Use null for autonyms (native names)
 687+ * @param $include string:
 688+ * 'all' all available languages
 689+ * 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames
 690+ * 'mwfile' only if the language is in 'mw' *and* has a message file
 691+ * @return array|false: language code => language name, false if $include is wrong
 692+ */
 693+ public static function fetchLanguageNames( $inLanguage = null, $include = 'all' ) {
 694+ global $wgExtraLanguageNames;
 695+ static $coreLanguageNames;
 696+
 697+ if ( $coreLanguageNames === null ) {
 698+ include( MWInit::compiledPath( 'languages/Names.php' ) );
 699+ }
 700+
697701 $names = array();
698 - wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $code ) );
699702
700 - foreach ( self::getLanguageNames() as $code => $name ) {
701 - if ( !isset( $names[$code] ) ) $names[$code] = $name;
 703+ if( $inLanguage ) {
 704+ # TODO: also include when $inLanguage is null, when this code is more efficient
 705+ wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) );
702706 }
703707
704 - return $names;
 708+ $mwNames = $wgExtraLanguageNames + $coreLanguageNames;
 709+ foreach ( $mwNames as $mwCode => $mwName ) {
 710+ # - Prefer own MediaWiki native name when not using the hook
 711+ # TODO: prefer it always to make it consistent, but casing is different in CLDR
 712+ # - For other names just add if not added through the hook
 713+ if ( ( $mwCode === $inLanguage && !$inLanguage ) || !isset( $names[$mwCode] ) ) {
 714+ $names[$mwCode] = $mwName;
 715+ }
 716+ }
 717+
 718+ if ( $include === 'all' ) {
 719+ return $names;
 720+ }
 721+
 722+ $returnMw = array();
 723+ $coreCodes = array_keys( $mwNames );
 724+ foreach( $coreCodes as $coreCode ) {
 725+ $returnMw[$coreCode] = $names[$coreCode];
 726+ }
 727+
 728+ if( $include === 'mw' ) {
 729+ return $returnMw;
 730+ } elseif( $include === 'mwfile' ) {
 731+ $namesMwFile = array();
 732+ # We do this using a foreach over the codes instead of a directory
 733+ # loop so that messages files in extensions will work correctly.
 734+ foreach ( $returnMw as $code => $value ) {
 735+ if ( is_readable( self::getMessagesFileName( $code ) ) ) {
 736+ $namesMwFile[$code] = $names[$code];
 737+ }
 738+ }
 739+ return $namesMwFile;
 740+ }
 741+ return false;
705742 }
706743
707744 /**
 745+ * @param $code string: The code of the language for which to get the name
 746+ * @param $inLanguage null|string: Code of language in which to return the name (null for autonyms)
 747+ * @return string: Language name or empty
 748+ * @since 1.20
 749+ */
 750+ public static function fetchLanguageName( $code, $inLanguage = null ) {
 751+ $array = self::fetchLanguageNames( $inLanguage, 'all' );
 752+ return !array_key_exists( $code, $array ) ? '' : $array[$code];
 753+ }
 754+
 755+ /**
708756 * Get a message from the MediaWiki namespace.
709757 *
710758 * @param $msg String: message name
@@ -718,13 +766,10 @@
719767 * Only if defined in MediaWiki, no other data like CLDR.
720768 * @param $code string
721769 * @return string
 770+ * @deprecated in 1.20, use fetchLanguageName()
722771 */
723772 function getLanguageName( $code ) {
724 - $names = self::getLanguageNames();
725 - if ( !array_key_exists( $code, $names ) ) {
726 - return '';
727 - }
728 - return $names[$code];
 773+ return self::fetchLanguageName( $code );
729774 }
730775
731776 /**
Index: trunk/extensions/Translate/TranslateUtils.php
@@ -187,8 +187,8 @@
188188 * @return array
189189 */
190190 public static function getLanguageNames( /*string */ $code ) {
191 - if ( is_callable( array( 'Language', 'getTranslatedLanguageNames' ) ) ) {
192 - return Language::getTranslatedLanguageNames( $code );
 191+ if ( is_callable( array( 'Language', 'fetchLanguageNames' ) ) ) {
 192+ return Language::fetchLanguageNames( $code, 'mw' ); // since 1.20
193193 } elseif ( is_callable( array( 'LanguageNames', 'getNames' ) ) ) {
194194 return LanguageNames::getNames( $code,
195195 LanguageNames::FALLBACK_NORMAL,

Follow-up revisions

RevisionCommit summaryAuthorDate
r112823fix typo in r112479robin17:57, 1 March 2012
r113385Follow-up r112479...robin19:30, 8 March 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r107002follow-up to r106912, making change to correct branch this timekaldari22:54, 21 December 2011

Comments

#Comment by Siebrand (talk | contribs)   12:18, 27 February 2012

Translate needs to remain compatible with the latest stable, which is 1.18.

#Comment by SPQRobin (talk | contribs)   12:27, 27 February 2012

When fetchLanguageNames is not found, it takes LanguageNames::getNames, i.e. CLDR directly, so it doesn't need to use getTranslatedLanguageNames. (It's even better this way, because the mentioned problem was present when calling getTranslatedLanguageNames.)

Confirmed that it works using a 1.18 checkout with trunk Translate.

#Comment by Siebrand (talk | contribs)   13:13, 27 February 2012

Thanks, Robin.

#Comment by Nikerabbit (talk | contribs)   19:25, 7 March 2012

Some indentation issues like in docs of fetchLanguageNames and it's missing @since.

#Comment by SPQRobin (talk | contribs)   19:32, 8 March 2012

Thought I added @since..

Done in r113385. Are there other indentation issues?

Status & tagging log