Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

Originally discovered as a problem with the 'GridFieldDetailForm.Saved' in nl.yml
  • Loading branch information...
commit f0f83b26a8c901f78a4e6ae9b7ff9641613cc61c 1 parent 661a4a2
@chillu chillu authored
Showing with 16 additions and 1 deletion.
  1. +7 −1 i18n/i18n.php
  2. +9 −0 tests/i18n/i18nTest.php
View
8 i18n/i18n.php
@@ -1536,7 +1536,13 @@ public static function _t($entity, $string = "", $context = "", $injection = "")
// 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
9 tests/i18n/i18nTest.php
@@ -301,6 +301,15 @@ public function testNewTMethodSignature() {
);
$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")
);
Please sign in to comment.
Something went wrong with that request. Please try again.