mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
661a4a2492
commit
f0f83b26a8
@ -1536,7 +1536,13 @@ class i18n extends Object implements TemplateGlobalProvider {
|
|||||||
// Legacy mode: If no injection placeholders are found,
|
// Legacy mode: If no injection placeholders are found,
|
||||||
// replace sprintf placeholders in fixed order.
|
// replace sprintf placeholders in fixed order.
|
||||||
// Fail silently in case the translation is outdated
|
// 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;
|
if($replaced) $returnValue = $replaced;
|
||||||
} else if(!ArrayLib::is_associative($injectionArray)) {
|
} else if(!ArrayLib::is_associative($injectionArray)) {
|
||||||
// Legacy mode: If injection placeholders are found,
|
// Legacy mode: If injection placeholders are found,
|
||||||
|
@ -300,6 +300,15 @@ class i18nTest extends SapphireTest {
|
|||||||
$translated, "Testing sprintf placeholders with named injections"
|
$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(
|
$translated = i18n::_t(
|
||||||
'i18nTestModule.INJECTIONS', // has {name} placeholders
|
'i18nTestModule.INJECTIONS', // has {name} placeholders
|
||||||
array("Cat", "meow", "meow")
|
array("Cat", "meow", "meow")
|
||||||
|
Loading…
Reference in New Issue
Block a user