Skip to content
Snippets Groups Projects
Commit f3ed2ebb authored by Demian Katz's avatar Demian Katz
Browse files

Revised translate view helper to use trait.

- Expanded trait functionality to match view helper.
parent 24417781
No related merge requests found
...@@ -89,16 +89,23 @@ trait TranslatorAwareTrait ...@@ -89,16 +89,23 @@ trait TranslatorAwareTrait
/** /**
* Translate a string * Translate a string
* *
* @param string $str String to translate * @param string $str String to translate
* @param array $tokens Tokens to inject into the translated string * @param array $tokens Tokens to inject into the translated string
* @param string $default Default value to use if no translation is found (null
* for no default).
* *
* @return string * @return string
*/ */
public function translate($str, $tokens = array()) public function translate($str, $tokens = array(), $default = null)
{ {
$msg = null === $this->translator $msg = null === $this->translator
? $str : $this->translator->translate($str); ? $str : $this->translator->translate($str);
// Did the translation fail to change anything? If so, use default:
if (null !== $default && $msg == $str) {
$msg = $default;
}
// Do we need to perform substitutions? // Do we need to perform substitutions?
if (!empty($tokens)) { if (!empty($tokens)) {
$in = $out = array(); $in = $out = array();
......
...@@ -26,8 +26,6 @@ ...@@ -26,8 +26,6 @@
* @link http://vufind.org/wiki/vufind2:developer_manual Wiki * @link http://vufind.org/wiki/vufind2:developer_manual Wiki
*/ */
namespace VuFind\View\Helper\Root; namespace VuFind\View\Helper\Root;
use Zend\I18n\Exception\RuntimeException,
Zend\I18n\View\Helper\AbstractTranslatorHelper;
/** /**
* Translate view helper * Translate view helper
...@@ -38,8 +36,11 @@ use Zend\I18n\Exception\RuntimeException, ...@@ -38,8 +36,11 @@ use Zend\I18n\Exception\RuntimeException,
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/vufind2:developer_manual Wiki * @link http://vufind.org/wiki/vufind2:developer_manual Wiki
*/ */
class Translate extends AbstractTranslatorHelper class Translate extends \Zend\View\Helper\AbstractHelper
implements \VuFind\I18n\Translator\TranslatorAwareInterface
{ {
use \VuFind\I18n\Translator\TranslatorAwareTrait;
/** /**
* Translate a string * Translate a string
* *
...@@ -52,33 +53,6 @@ class Translate extends AbstractTranslatorHelper ...@@ -52,33 +53,6 @@ class Translate extends AbstractTranslatorHelper
*/ */
public function __invoke($str, $tokens = array(), $default = null) public function __invoke($str, $tokens = array(), $default = null)
{ {
try { return $this->translate($str, $tokens, $default);
$translator = $this->getTranslator();
if (!is_object($translator)) {
throw new RuntimeException();
}
$msg = $translator->translate($str);
} catch (RuntimeException $e) {
// If we get called before the translator is set up, it will throw an
// exception, but we should still try to display some text!
$msg = $str;
}
// Did the translation fail to change anything? If so, use default:
if (!is_null($default) && $msg == $str) {
$msg = $default;
}
// Do we need to perform substitutions?
if (!empty($tokens)) {
$in = $out = array();
foreach ($tokens as $key => $value) {
$in[] = $key;
$out[] = $value;
}
$msg = str_replace($in, $out, $msg);
}
return $msg;
} }
} }
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment