BUG Graceful handling of sprintf with too few params in i18n::_t()

Originally discovered as a problem with the 'GridFieldDetailForm.Saved' in nl.yml
This commit is contained in:
Ingo Schommer 2012-12-21 11:46:30 +01:00
parent 661a4a2492
commit f0f83b26a8
2 changed files with 16 additions and 1 deletions

View File

@ -1536,7 +1536,13 @@ class i18n extends Object implements TemplateGlobalProvider {
// Legacy mode: If no injection placeholders are found,
// replace sprintf placeholders in fixed order.
// Fail silently in case the translation is outdated
$replaced = @vsprintf($returnValue, array_values($injectionArray));
preg_match_all('/%[s,d]/', $returnValue, $returnValueArgs);
if($returnValueArgs) foreach($returnValueArgs[0] as $i => $returnValueArg) {
if($i >= count($injectionArray)) {
$injectionArray[] = '';
}
}
$replaced = vsprintf($returnValue, array_values($injectionArray));
if($replaced) $returnValue = $replaced;
} else if(!ArrayLib::is_associative($injectionArray)) {
// Legacy mode: If injection placeholders are found,

View File

@ -300,6 +300,15 @@ class i18nTest extends SapphireTest {
$translated, "Testing sprintf placeholders with named injections"
);
$translated = i18n::_t(
'i18nTestModule.INJECTIONSLEGACY', // has %s placeholders
array("Cat", "meow"/*, "meow" */) // remove third arg
);
$this->assertContains(
"TRANS Hello Cat meow. But it is late, ",
$translated, "Testing sprintf placeholders with unnamed injections and too few args"
);
$translated = i18n::_t(
'i18nTestModule.INJECTIONS', // has {name} placeholders
array("Cat", "meow", "meow")