https://www.mediawiki.org/wiki/Special:Code/MediaWiki/112171

Revision: 112171
Author:   pgehres
Date:     2012-02-23 00:41:12 +0000 (Thu, 23 Feb 2012)
Log Message:
-----------
Updating wmfMessageExists to actully do what we want.  Adding 
wfLangSpecificFallback

Modified Paths:
--------------
    trunk/extensions/DonationInterface/gateway_common/DataValidator.php

Modified: trunk/extensions/DonationInterface/gateway_common/DataValidator.php
===================================================================
--- trunk/extensions/DonationInterface/gateway_common/DataValidator.php 
2012-02-23 00:39:15 UTC (rev 112170)
+++ trunk/extensions/DonationInterface/gateway_common/DataValidator.php 
2012-02-23 00:41:12 UTC (rev 112171)
@@ -238,10 +238,59 @@
         * @return boolean - true if message exists, otherwise false.
         */
        public static function wmfMessageExists( $msg_key, $language ){
-               return wfMessage( $msg_key )->inLanguage( $language )->exists();
+               if ( wfMessage( $msg_key )->inLanguage( $language )->exists() ){
+                       # if we are looking for English, we already know the 
answer
+                       if ( $language == 'en' ){
+                               return true;
+                       }
+
+                       # get the english version of the message
+                       $msg_en = wfMessage( $msg_key )->inLanguage( 'en' 
)->text();
+                       # attempt to get the message in the specified language
+                       $msg_lang = wfMessage( $msg_key )->inLanguage( 
$language )->text();
+
+                       # if the messages are the same, the message fellback to 
English, return false
+                       return strcmp( $msg_en, $msg_lang ) != 0;
+               }
+               return false;
        }
 
+       /**
+        * wfLangSpecificFallback - returns the text of the first existant 
message
+        * in the requested language. If no messages are found in that 
language, the
+        * function returns the first existant fallback message.
+        *
+        * @param $language the requested language
+        * @return String the text of the first existant message
+        * @throws MWException if no message keys are specified
+        */
+       public static function wfLangSpecificFallback( $language /*...*/ ){
+               $msg_keys = func_get_args();
+               array_shift( $msg_keys );
 
+               if ( count( $msg_keys ) < 1 ){
+                       throw new MWException( __FUNCTION__ . " BAD PROGRAMMER. 
No message keys given." );
+               }
+
+               # look for the first message that exists
+               foreach ( $msg_keys as $m ){
+                       if ( self::wmfMessageExists( $m, $language) ){
+                               return wfMessage( $m )->inLanguage( $language 
)->text();
+                       }
+               }
+
+               # we found nothing in the requested language, return the first 
fallback message that exists
+               foreach ( $msg_keys as $m ){
+                       if ( wfMessage( $m )->inLanguage( $language )->exists() 
){
+                               return wfMessage( $m )->inLanguage( $language 
)->text();
+                       }
+               }
+
+               # somehow we still don't have a message, return a default error 
message
+               return wfMessage( $msg_keys[0] )->text();
+       }
+
+
        /**
         * validate
         * Run all the validation rules we have defined against a (hopefully 


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to