Skip to content

Instantly share code, notes, and snippets.

@MasonM
Created October 19, 2011 17:31
Show Gist options
  • Save MasonM/1299031 to your computer and use it in GitHub Desktop.
Save MasonM/1299031 to your computer and use it in GitHub Desktop.
Change Zend_View_Helper_Translate->translate() to not treat last parameter as a locale if it would result in errors
Index: tests/Zend/View/Helper/TranslateTest.php
===================================================================
--- tests/Zend/View/Helper/TranslateTest.php (revision 24514)
+++ tests/Zend/View/Helper/TranslateTest.php (working copy)
@@ -206,6 +206,7 @@
$this->helper->setTranslator($trans);
$this->assertEquals("drei 100 200", $this->helper->translate("three %1\$s %2\$s", "100", "200"));
$this->assertEquals("tre 100 200", $this->helper->translate("three %1\$s %2\$s", "100", "200", 'it'));
+ $this->assertEquals("drei 100 it", $this->helper->translate("three %1\$s %2\$s", "100", "it"));
$this->assertEquals("drei 100 200", $this->helper->translate("three %1\$s %2\$s", array("100", "200")));
$this->assertEquals("tre 100 200", $this->helper->translate("three %1\$s %2\$s", array("100", "200"), 'it'));
}
Index: library/Zend/View/Helper/Translate.php
===================================================================
--- library/Zend/View/Helper/Translate.php (revision 24514)
+++ library/Zend/View/Helper/Translate.php (working copy)
@@ -79,7 +79,10 @@
$locale = null;
if ($count > 0) {
if (Zend_Locale::isLocale($options[($count - 1)], null, false) !== false) {
- $locale = array_pop($options);
+ // Don't treat last option as the locale if doing so will result in an error
+ if (is_array($options[0]) || @vsprintf($messageid, array_slice($options, 0, -1)) !== false) {
+ $locale = array_pop($options);
+ }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment