Fix: Regression with format specifiers in i18n methods

Refs: #2589
This commit is contained in:
Rachman Chavik 2014-03-07 09:13:23 +07:00
parent 6c3bc48ce0
commit e71d650ade
4 changed files with 154 additions and 7 deletions

View file

@ -432,6 +432,146 @@ class BasicsTest extends CakeTestCase {
$this->assertEquals($expected, $result, 'significant-digit placeholder should not be misinterpreted');
}
/**
* 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()
*

View file

@ -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)"

View file

@ -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 file

@ -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);
}