diff --git a/i18n/i18n.php b/i18n/i18n.php index 98f1c6904..f714cf46b 100644 --- a/i18n/i18n.php +++ b/i18n/i18n.php @@ -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, diff --git a/tests/i18n/i18nTest.php b/tests/i18n/i18nTest.php index 391e5a72d..07dbb7162 100644 --- a/tests/i18n/i18nTest.php +++ b/tests/i18n/i18nTest.php @@ -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")