handle loose % signs in __() function

While it's passed to sprintf and translation markers may contain
variable placeholders (%s, %d) that doesn't mean translators need to be
aware of all relevant rules. A % sign in a translation message shouldn't
cause a warning (and no return) it should be handled as a literal
percent sign.
This commit is contained in:
AD7six 2014-01-02 16:04:29 +00:00
parent 9de3418079
commit 5334fe04c3
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);
}