[pLog-svn] r5767 - in plog/trunk/class: locale test/tests/locale

oscar at devel.lifetype.net oscar at devel.lifetype.net
Wed Aug 1 16:07:00 EDT 2007


Author: oscar
Date: 2007-08-01 16:07:00 -0400 (Wed, 01 Aug 2007)
New Revision: 5767

Modified:
   plog/trunk/class/locale/locale.class.php
   plog/trunk/class/test/tests/locale/locale_test.class.php
Log:
Implement Locale::pr() properly to support variable-length arguments (and not the hack that we had before...)


Modified: plog/trunk/class/locale/locale.class.php
===================================================================
--- plog/trunk/class/locale/locale.class.php	2007-08-01 13:41:15 UTC (rev 5766)
+++ plog/trunk/class/locale/locale.class.php	2007-08-01 20:07:00 UTC (rev 5767)
@@ -274,26 +274,23 @@
 		}
 
 		/**
-		 * calls printf on the translated string.
-		 *
-		 * Crappy Crappy! Since it only accepts three arguments... ;) Well, if we
-		 * ever need more than three, I'll change it!
+		 * Improved version of Locale::tr that supports variable length arguments.
+		 * In order to refer to these arguments in the locale file, use %1$s, %2$s,  
+		 * etc.
          * @private
 		 */
-		function pr( $id, $arg1 = null, $arg2 = null, $arg3 = null )
+		function pr( $id )
 		{
-			// first of all, we translate the string
+			// the first parameter contains the id of the string
 			$str = $this->tr( $id );
-			if( $arg1 == null )
-				$result = $str;
-			else if( $arg2 == null )
-				$result = sprintf( $str, $arg1 );
-			else if( $arg3 == null )
-				$result = sprintf( $str, $arg1, $arg2 );
-			else
-				$result = sprintf( $str, $arg1, $arg2, $arg3 );
+			
+			for( $i = 1; $i < func_num_args(); $i++ ) {
+				$strId = "%{$i}\$s";
+				$param = func_get_arg( $i ) ;
+				$str = str_replace( $strId, $param, $str );
+			}
 
-			return $result;
+			return $str;
 		}
 
         /**

Modified: plog/trunk/class/test/tests/locale/locale_test.class.php
===================================================================
--- plog/trunk/class/test/tests/locale/locale_test.class.php	2007-08-01 13:41:15 UTC (rev 5766)
+++ plog/trunk/class/test/tests/locale/locale_test.class.php	2007-08-01 20:07:00 UTC (rev 5767)
@@ -20,6 +20,20 @@
 			$this->l = new Locale( "en_UK" );
 		}
 		
+		/** 
+		 * Tests that the Locale::pr() function works correctly
+		 * with variable length arguments
+		 */
+		function testPr()
+		{	
+			$this->l->_loadLocaleFile();
+			$this->l->_messages["pr_test_1"] = "%1\$s, %2\$s!";
+			$this->l->_messages["pr_test_2"] = "%1\$s, how %2\$s %3\$s today, %4\$s?";
+			
+			$this->assertEquals( "Hello, world!", $this->l->pr( "pr_test_1", "Hello", "world" ));
+			$this->assertEquals( "Hello, how are you today, sir?", $this->l->pr( "pr_test_2", "Hello", "are", "you", "sir" ));			
+		}
+		
 		/**
 		 * test all the modifiers from the Locale::formatDate() method:
 		 *



More information about the pLog-svn mailing list