Merge pull request #2589 from AD7six/handle-percent-translations

Handle percent symbol in translation functions
This commit is contained in:
Andy Dawson 2014-02-13 16:48:43 +01:00
commit 198f8b6df5
2 changed files with 49 additions and 0 deletions

View file

@ -397,6 +397,41 @@ class BasicsTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* testTranslatePercent
*
* @return void
*/
public function testTranslatePercent() {
$result = __('%s are 100% real fruit', 'Apples');
$expected = 'Apples are 100% real fruit';
$this->assertEquals($expected, $result, 'Percent sign at end of word should be considered literal');
$result = __('%s are %d% real fruit', 'Apples', 100);
$expected = 'Apples are 100% real fruit';
$this->assertEquals($expected, $result, 'A digit marker should not be misinterpreted');
$result = __('%s are %s% real fruit', 'Apples', 100);
$expected = 'Apples are 100% real fruit';
$this->assertEquals($expected, $result, 'A string marker should not be misinterpreted');
$result = __('%nonsense %s', 'Apples');
$expected = '%nonsense Apples';
$this->assertEquals($expected, $result, 'A percent sign at the start of the string should be considered literal');
$result = __('%s are awesome%', 'Apples');
$expected = 'Apples are awesome%';
$this->assertEquals($expected, $result, 'A percent sign at the end of the string should be considered literal');
$result = __('%2$d %1$s entered the bowl', 'Apples', 2);
$expected = '2 Apples entered the bowl';
$this->assertEquals($expected, $result, 'Positional replacement markers should not be misinterpreted');
$result = __('%.2f% of all %s agree', 99.44444, 'Cats');
$expected = '99.44% of all Cats agree';
$this->assertEquals($expected, $result, 'significant-digit placeholder should not be misinterpreted');
}
/**
* test __n()
*

View file

@ -559,6 +559,8 @@ if (!function_exists('__')) {
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 1);
}
$translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}
@ -589,6 +591,8 @@ if (!function_exists('__n')) {
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 3);
}
$translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}
@ -616,6 +620,8 @@ if (!function_exists('__d')) {
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 2);
}
$translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}
@ -647,6 +653,8 @@ if (!function_exists('__dn')) {
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 4);
}
$translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}
@ -689,6 +697,8 @@ if (!function_exists('__dc')) {
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 3);
}
$translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}
@ -735,6 +745,8 @@ if (!function_exists('__dcn')) {
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 5);
}
$translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}
@ -773,6 +785,8 @@ if (!function_exists('__c')) {
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 2);
}
$translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}