public function testMessageIsTranslated()
 {
     $message = "An awesome message";
     $expectedMessage = "Translated message";
     $parameters = array('foo' => 'bar,');
     $domain = "RedKiteCms";
     $locale = 'it';
     $translator = $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface');
     $translator->expects($this->once())->method('trans')->with($message, $parameters, $domain, $locale)->will($this->returnValue($expectedMessage));
     Translator::setTranslator($translator);
     $translatedMessage = Translator::translate($message, $parameters, $domain, $locale);
     $this->assertEquals($translatedMessage, $expectedMessage);
 }
Пример #2
0
 public static function translateException($message, \Exception $exception = null)
 {
     $jsonMessage = json_decode($message, true);
     if (!is_array($jsonMessage)) {
         $jsonMessage = array('message' => $message);
     }
     $parameters = array('message' => '', 'parameters' => array(), 'domain' => 'RedKiteCms', 'locale' => null);
     $cleanedParameters = array_intersect_key($jsonMessage, $parameters);
     $parameters = array_merge($parameters, $cleanedParameters);
     $message = Translator::translate($parameters["message"], $parameters["parameters"], $parameters["domain"], $parameters["locale"]);
     if (null !== $exception && array_key_exists("show_exception", $jsonMessage) && $jsonMessage["show_exception"]) {
         $message = substr(strrchr(get_class($exception), '\\'), 1) . ": " . $message;
     }
     return $message;
 }
 /**
  * Translates the block
  */
 protected function translate()
 {
     $translatorOptions = $this->getTranslatorOptions();
     if (empty($translatorOptions) && !array_key_exists("fields", $translatorOptions)) {
         return;
     }
     $params = array();
     if (array_key_exists("params", $translatorOptions)) {
         $params = $translatorOptions["params"];
     }
     $domain = "RedKiteCms";
     if (array_key_exists("domain", $translatorOptions)) {
         $domain = $translatorOptions["domain"];
     }
     foreach ($translatorOptions["fields"] as $field) {
         $field = ucfirst($field);
         $method = 'get' . $field;
         $value = Translator::translate($this->{$method}(), $params, $domain);
         $method = 'set' . $field;
         $this->{$method}($value);
     }
 }