Browse Source

Fix: Regression with format specifiers in i18n methods

Refs: #2589
Rachman Chavik 12 years ago
parent
commit
e71d650ade

+ 140 - 0
lib/Cake/Test/Case/BasicsTest.php

@@ -433,6 +433,146 @@ class BasicsTest extends CakeTestCase {
 	}
 
 /**
+ * testTranslateWithFormatSpecifiers
+ *
+ * @return void
+ */
+	public function testTranslateWithFormatSpecifiers() {
+		$expected = 'Check,   one, two, three';
+		$result = __('Check, %+10s, three', 'one, two');
+		$this->assertEquals($expected, $result);
+
+		$expected = 'Check,    +1, two, three';
+		$result = __('Check, %+5d, two, three', 1);
+		$this->assertEquals($expected, $result);
+
+		$expected = 'Check, @@one, two, three';
+		$result = __('Check, %\'@+10s, three', 'one, two');
+		$this->assertEquals($expected, $result);
+
+		$expected = 'Check, one, two  , three';
+		$result = __('Check, %-10s, three', 'one, two');
+		$this->assertEquals($expected, $result);
+
+		$expected = 'Check, one, two##, three';
+		$result = __('Check, %\'#-10s, three', 'one, two');
+		$this->assertEquals($expected, $result);
+
+		$expected = 'Check,   one, two, three';
+		$result = __d('default', 'Check, %+10s, three', 'one, two');
+		$this->assertEquals($expected, $result);
+
+		$expected = 'Check, @@one, two, three';
+		$result = __d('default', 'Check, %\'@+10s, three', 'one, two');
+		$this->assertEquals($expected, $result);
+
+		$expected = 'Check, one, two  , three';
+		$result = __d('default', 'Check, %-10s, three', 'one, two');
+		$this->assertEquals($expected, $result);
+
+		$expected = 'Check, one, two##, three';
+		$result = __d('default', 'Check, %\'#-10s, three', 'one, two');
+		$this->assertEquals($expected, $result);
+	}
+
+/**
+ * testTranslateDomainPluralWithFormatSpecifiers
+ *
+ * @return void
+ */
+	public function testTranslateDomainPluralWithFormatSpecifiers() {
+		$result = __dn('core', '%+5d item.', '%+5d items.', 1, 1);
+		$expected = '   +1 item.';
+		$this->assertEquals($expected, $result);
+
+		$result = __dn('core', '%-5d item.', '%-5d items.', 10, 10);
+		$expected = '10    items.';
+		$this->assertEquals($expected, $result);
+
+		$result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 1, 1);
+		$expected = '###+1 item.';
+		$this->assertEquals($expected, $result);
+
+		$result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 90, 90);
+		$expected = '**+90 items.';
+		$this->assertEquals($expected, $result);
+
+		$result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 9000, 9000);
+		$expected = '+9000 items.';
+		$this->assertEquals($expected, $result);
+	}
+
+/**
+ * test testTranslatePluralWithFormatSpecifiers
+ *
+ * @return void
+ */
+	public function testTranslatePluralWithFormatSpecifiers() {
+		Configure::write('Config.language', 'rule_1_po');
+
+		$result = __n('%-5d = 1', '%-5d = 0 or > 1', 10);
+		$expected = '%-5d = 0 or > 1 (translated)';
+		$this->assertEquals($expected, $result);
+	}
+
+/**
+ * test testTranslateDomainCategoryWithFormatSpecifiers
+ *
+ * @return void
+ */
+	public function testTranslateDomainCategoryWithFormatSpecifiers() {
+		Configure::write('Config.language', 'rule_1_po');
+
+		$result = __dc('default', '%+10s world', 6, 'hello');
+		$expected = '     hello world';
+		$this->assertEquals($expected, $result);
+
+		$result = __dc('default', '%-10s world', 6, 'hello');
+		$expected = 'hello      world';
+		$this->assertEquals($expected, $result);
+
+		$result = __dc('default', '%\'@-10s world', 6, 'hello');
+		$expected = 'hello@@@@@ world';
+		$this->assertEquals($expected, $result);
+	}
+
+/**
+ * test testTranslateDomainCategoryPluralWithFormatSpecifiers
+ *
+ * @return void
+ */
+	public function testTranslateDomainCategoryPluralWithFormatSpecifiers() {
+		Configure::write('Config.language', 'rule_1_po');
+
+		$result = __dcn('default', '%-5d = 1', '%-5d = 0 or > 1', 0, 6);
+		$expected = '%-5d = 0 or > 1 (translated)';
+		$this->assertEquals($expected, $result);
+
+		$result = __dcn('default', '%-5d = 1', '%-5d = 0 or > 1', 1, 6);
+		$expected = '%-5d = 1 (translated)';
+		$this->assertEquals($expected, $result);
+	}
+
+/**
+ * test testTranslateCategoryWithFormatSpecifiers
+ *
+ * @return void
+ */
+	public function testTranslateCategoryWithFormatSpecifiers() {
+		$result = __c('Some string with %+10s', 6, 'arguments');
+		$expected = 'Some string with  arguments';
+		$this->assertEquals($expected, $result);
+
+		$result = __c('Some string with %-10s: args', 6, 'arguments');
+		$expected = 'Some string with arguments : args';
+		$this->assertEquals($expected, $result);
+
+		$result = __c('Some string with %\'*-10s: args', 6, 'arguments');
+		$expected = 'Some string with arguments*: args';
+		$this->assertEquals($expected, $result);
+	}
+
+/**
  * test __n()
  *
  * @return void

+ 2 - 0
lib/Cake/Test/test_app/Locale/rule_13_po/LC_MESSAGES/core.po

@@ -22,3 +22,5 @@ msgstr[1] "%d is 0 or ends in 01-10 (from core translated)"
 msgstr[2] "%d ends in 11-20 (from core translated)"
 msgstr[3] "%d everything else (from core translated)"
 
+msgid "%+5d = 1 (from core)"
+msgid_plural "%+5d = 0 or > 1 (from core)"

+ 5 - 0
lib/Cake/Test/test_app/Locale/rule_1_po/LC_MESSAGES/default.po

@@ -20,6 +20,11 @@ msgid_plural "%d = 0 or > 1"
 msgstr[0] "%d = 1 (translated)"
 msgstr[1] "%d = 0 or > 1 (translated)"
 
+msgid "%-5d = 1"
+msgid_plural "%-5d = 0 or > 1"
+msgstr[0] "%-5d = 1 (translated)"
+msgstr[1] "%-5d = 0 or > 1 (translated)"
+
 #~ msgid "Plural-Forms 1"
 #~ msgstr "Plural-Forms 1 (translated)"
 

+ 7 - 7
lib/Cake/basics.php

@@ -560,7 +560,7 @@ if (!function_exists('__')) {
 			$args = array_slice(func_get_args(), 1);
 		}
 
-		$translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
+		$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
 		return vsprintf($translated, $args);
 	}
 
@@ -592,7 +592,7 @@ if (!function_exists('__n')) {
 			$args = array_slice(func_get_args(), 3);
 		}
 
-		$translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
+		$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
 		return vsprintf($translated, $args);
 	}
 
@@ -621,7 +621,7 @@ if (!function_exists('__d')) {
 			$args = array_slice(func_get_args(), 2);
 		}
 
-		$translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
+		$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
 		return vsprintf($translated, $args);
 	}
 
@@ -654,7 +654,7 @@ if (!function_exists('__dn')) {
 			$args = array_slice(func_get_args(), 4);
 		}
 
-		$translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
+		$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
 		return vsprintf($translated, $args);
 	}
 
@@ -698,7 +698,7 @@ if (!function_exists('__dc')) {
 			$args = array_slice(func_get_args(), 3);
 		}
 
-		$translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
+		$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
 		return vsprintf($translated, $args);
 	}
 
@@ -746,7 +746,7 @@ if (!function_exists('__dcn')) {
 			$args = array_slice(func_get_args(), 5);
 		}
 
-		$translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
+		$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
 		return vsprintf($translated, $args);
 	}
 
@@ -786,7 +786,7 @@ if (!function_exists('__c')) {
 			$args = array_slice(func_get_args(), 2);
 		}
 
-		$translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
+		$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
 		return vsprintf($translated, $args);
 	}