Example #1
0
 /**
  * Translate given string
  * @param string $message
  * @param int $form plural form (positive number)
  * @return string
  */
 public function translate($message, $form = 1)
 {
     $this->loadDictonary();
     $message = (string) $message;
     $message_plural = NULL;
     if (is_array($form) && $form !== NULL) {
         $message_plural = current($form);
         $form = (int) end($form);
     } elseif (is_numeric($form)) {
         $form = (int) $form;
         if ($form < 2) {
             $message_plural = 0;
         } elseif ($form < 5) {
             $message_plural = 1;
         } else {
             $message_plural = 2;
         }
     } elseif (!is_int($form) || $form === NULL) {
         $form = 1;
     }
     if (!empty($message) && isset($this->dictionary[$message])) {
         $tmp = preg_replace('/([a-z]+)/', '$$1', "n={$form};" . $this->metadata[$this->dictionary[$message]['file']]['Plural-Forms']);
         eval($tmp);
         $message = $this->dictionary[$message]['translation'];
         if (!empty($message)) {
             $message = is_array($message) && $message_plural !== NULL && isset($message[$message_plural]) ? $message[$message_plural] : $message;
         }
     } else {
         if ($this->debugMode === true && (!$this->httpResponse->isSent() || $this->sessionStorage)) {
             if (!isset($this->sessionStorage->newStrings[$this->lang])) {
                 $this->sessionStorage->newStrings[$this->lang] = array();
             }
             $this->sessionStorage->newStrings[$this->lang][$message] = empty($message_plural) ? array($message) : array($message, $message_plural);
         }
         if ($form > 1 && !empty($message_plural)) {
             $message = $message_plural;
         }
     }
     if (is_array($message)) {
         $message = current($message);
     }
     $args = func_get_args();
     if (count($args) > 1) {
         array_shift($args);
         if (is_array(current($args)) || current($args) === NULL) {
             array_shift($args);
         }
         if (count($args) == 1 && is_array(current($args))) {
             $args = current($args);
         }
         $message = str_replace(array('%label', '%name', '%value'), array('#label', '#name', '#value'), $message);
         if (count($args) > 0 && $args != NULL) {
             $message = vsprintf($message, $args);
         }
         $message = str_replace(array('#label', '#name', '#value'), array('%label', '%name', '%value'), $message);
     }
     return $message;
 }
Example #2
0
 /**
  * @return Nette\Http\Response
  */
 public static function createServiceHttpResponse()
 {
     $response = new Nette\Http\Response();
     if (!$response->isSent()) {
         $response->setContentType('text/html', 'utf-8');
     }
     return $response;
 }