Permalink
Browse files

Fix: Regression with format specifiers in i18n methods

Refs: #2589
  • Loading branch information...
1 parent 6c3bc48 commit e71d650ade7fd07f5038f178b287e659d43d7acd @rchavik rchavik committed Mar 7, 2014
@@ -433,6 +433,146 @@ public function testTranslatePercent() {
}
/**
+ * 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
@@ -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)"
@@ -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)"
View
@@ -560,7 +560,7 @@ function __($singular, $args = null) {
$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 @@ function __n($singular, $plural, $count, $args = null) {
$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 @@ function __d($domain, $msg, $args = null) {
$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 @@ function __dn($domain, $singular, $plural, $count, $args = null) {
$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 @@ function __dc($domain, $msg, $category, $args = null) {
$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 @@ function __dcn($domain, $singular, $plural, $count, $category, $args = null) {
$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 @@ function __c($msg, $category, $args = null) {
$args = array_slice(func_get_args(), 2);
}
- $translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
+ $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}

0 comments on commit e71d650

Please sign in to comment.