r34241 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r34240‎ | r34241 | r34242 >
Date:13:29, 5 May 2008
Author:minuteelectron
Status:old
Tags:
Comment:
* wfLoadExtensionMessages() now accepts a language code as its second argument, rather than a boolean indicating whether or not to load all messages. This allows extension developers to be more discriminate with regards to which languages they load, avoiding the problem of loading lots of message localisations and fall backs not being merged in.
Modified paths:
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)
  • /trunk/phase3/includes/MessageCache.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/GlobalFunctions.php
@@ -2479,16 +2479,29 @@
24802480 * Load an extension messages file
24812481 *
24822482 * @param string $extensionName Name of extension to load messages from\for.
2483 - * @param boolean $all Whether or not to load all languages.
 2483+ * @param string $langcode Language to load messages for, or false for default
 2484+ * behvaiour (en, content language and user language).
24842485 */
2485 -function wfLoadExtensionMessages( $extensionName, $all = false ) {
2486 - global $wgExtensionMessagesFiles, $wgMessageCache;
2487 - if ( !empty( $wgExtensionMessagesFiles[$extensionName] ) ) {
2488 - $wgMessageCache->loadMessagesFile( $wgExtensionMessagesFiles[$extensionName], $all );
2489 - // Prevent double-loading if all the messages have been loaded.
2490 - if( $all ) {
2491 - $wgExtensionMessagesFiles[$extensionName] = false;
2492 - }
 2486+function wfLoadExtensionMessages( $extensionName, $langcode = false ) {
 2487+ global $wgExtensionMessagesFiles, $wgMessageCache, $wgLang, $wgContLang;
 2488+
 2489+ #For recording whether extension message files have been loaded in a given language.
 2490+ static $loaded = array();
 2491+
 2492+ if( !$langcode && !array_key_exists( '*', $loaded ) ) {
 2493+ # Just do en, content language and user language.
 2494+ $wgMessageCache->loadMessagesFile( $wgExtensionMessagesFiles[$extensionName], false );
 2495+ # Mark that they have been loaded.
 2496+ $loaded['en'] = true;
 2497+ $loaded[$wgLang->getCode()] = true;
 2498+ $loaded[$wgContLang->getCode()] = true;
 2499+ # Mark that this part has been done to avoid weird if statements.
 2500+ $loaded['*'] = true;
 2501+ } elseif( is_string( $langcode ) && !array_key_exists( $langcode, $loaded ) ) {
 2502+ # Load messages for specified language.
 2503+ $wgMessageCache->loadMessagesFile( $wgExtensionMessagesFiles[$extensionName], $langcode );
 2504+ # Mark that they have been loaded.
 2505+ $loaded[$langcode] = true;
24932506 }
24942507 }
24952508
Index: trunk/phase3/includes/MessageCache.php
@@ -714,21 +714,21 @@
715715 * Load messages from a given file
716716 *
717717 * @param string $filename Filename of file to load.
718 - * @param boolean $all Whether or not to load all languages.
 718+ * @param string $langcode Language to load messages for, or false for
 719+ * default behvaiour (en, content language and user
 720+ * language).
719721 */
720 - function loadMessagesFile( $filename, $all = false ) {
 722+ function loadMessagesFile( $filename, $langcode = false ) {
721723 global $wgLang, $wgContLang;
722724 $messages = $magicWords = false;
723725 require( $filename );
724726
725 - if( $all ) {
726 - # Load all messages, regardless of language.
727 - $validCodes = array_keys( Language::getLanguageNames() );
728 - foreach( $messages as $code => $array ) {
729 - if( in_array( $code, $validCodes ) ) {
730 - $this->processMessagesArray( $messages, $code );
731 - }
732 - }
 727+ $validCodes = Language::getLanguageNames();
 728+ if( is_string( $langcode ) && array_key_exists( $langcode, $validCodes ) ) {
 729+ # Load messages for given language code.
 730+ $this->processMessagesArray( $messages, $langcode );
 731+ } elseif( is_string( $langcode ) && !array_key_exists( $langcode, $validCodes ) ) {
 732+ wfDebug( "Invalid language '$langcode' code passed to MessageCache::loadMessagesFile()" );
733733 } else {
734734 # Load only languages that are usually used, and merge all
735735 # fallbacks, except English.
@@ -748,10 +748,10 @@
749749 * Process an array of messages, loading it into the message cache.
750750 *
751751 * @param array $messages Messages array.
752 - * @param string $language Language code to process.
 752+ * @param string $langcode Language code to process.
753753 */
754 - function processMessagesArray( $messages, $language ) {
755 - $fallbackCode = $language;
 754+ function processMessagesArray( $messages, $langcode ) {
 755+ $fallbackCode = $langcode;
756756 $mergedMessages = array();
757757 do {
758758 if ( isset($messages[$fallbackCode]) ) {
@@ -761,7 +761,7 @@
762762 } while( $fallbackCode && $fallbackCode !== 'en' );
763763
764764 if ( !empty($mergedMessages) )
765 - $this->addMessages( $mergedMessages, $language );
 765+ $this->addMessages( $mergedMessages, $langcode );
766766 }
767767
768768 }

Status & tagging log