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

Jon Daley plogworld at jon.limedaley.com
Thu Aug 2 06:46:56 EDT 2007


 	Do we have to use %1$s instead of just %s?  I haven't seen that 
notation before, did you make it up or is that standard?
 	The reason I ask, is that I thought the first check-in was fine, 
but this second one looks like it could lead to problems - If someone 
happens to make a locale string with only one replacement, but uses %1$s, 
it won't work (if I read the code correctly).
 	I think if we didn't have any numbers but just used %s instead, 
that would be better.  The str_replace can't do it, but we could use 
strtok or strpos to do it a custom way, or use vsprintf.

On Wed, 1 Aug 2007, oscar at devel.lifetype.net wrote:

> Author: oscar
> Date: 2007-08-01 16:39:07 -0400 (Wed, 01 Aug 2007)
> New Revision: 5768
>
> Modified:
>   plog/trunk/class/locale/locale.class.php
>   plog/trunk/class/test/tests/locale/locale_test.class.php
> Log:
> We should also support %s, in additon to %1$s, %2$s, etc as many of our strings use %s instead of %1$s to specify positional parameters.
>
>
> Modified: plog/trunk/class/locale/locale.class.php
> ===================================================================
> --- plog/trunk/class/locale/locale.class.php	2007-08-01 20:07:00 UTC (rev 5767)
> +++ plog/trunk/class/locale/locale.class.php	2007-08-01 20:39:07 UTC (rev 5768)
> @@ -284,11 +284,17 @@
> 			// the first parameter contains the id of the string
> 			$str = $this->tr( $id );
>
> -			for( $i = 1; $i < func_num_args(); $i++ ) {
> -				$strId = "%{$i}\$s";
> -				$param = func_get_arg( $i ) ;
> -				$str = str_replace( $strId, $param, $str );
> +			if( func_num_args() == 2 ) {
> +				$param = func_get_arg( 1 ) ;
> +				$str = str_replace( "%s", $param, $str );
> 			}
> +			else {
> +				for( $i = 1; $i < func_num_args(); $i++ ) {
> +					$strId = "%{$i}\$s";
> +					$param = func_get_arg( $i ) ;
> +					$str = str_replace( $strId, $param, $str );
> +				}
> +			}
>
> 			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 20:07:00 UTC (rev 5767)
> +++ plog/trunk/class/test/tests/locale/locale_test.class.php	2007-08-01 20:39:07 UTC (rev 5768)
> @@ -29,9 +29,11 @@
> 			$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->l->_messages["pr_test_3"] = "Hello, %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" ));
> +			$this->assertEquals( "Hello, world!", $this->l->pr( "pr_test_3", "world" ));
> 		}
>
> 		/**
>
> _______________________________________________
> pLog-svn mailing list
> pLog-svn at devel.lifetype.net
> http://limedaley.com/mailman/listinfo/plog-svn
>

-- 
Jon Daley
http://jon.limedaley.com/

Inanimate objects are classified scientifically into
three major categories: those that don't work, those
that break down, and those that get lost.
-- Russell Baker


More information about the pLog-svn mailing list