示例#1
0
 /**
  * Translate a message.
  *
  * @param  string $message
  * @param  string $textDomain
  * @param  string $locale
  * @return string
  */
 public function __invoke($message, $textDomain = 'default', $locale = null)
 {
     if ($this->translator === null) {
         throw new Exception\RuntimeException('Translator has not been set');
     }
     return $this->translator->translate($message, $textDomain, $locale);
 }
示例#2
0
 public function translate($text)
 {
     if ($this->translator === null) {
         $this->translator = $this->getApplicationServiceLocator()->get('translator');
     }
     return $this->translator->translate($text);
 }
示例#3
0
 public function testTranslatorAddsPattern()
 {
     $translator = new Translator();
     $translator->addTranslationPattern('phparray', $this->testFilesDir . '/testarray', 'translation-%s.php');
     $this->assertEquals('Message 1 (en)', $translator->translate('Message 1', 'default', 'en_US'));
     $this->assertEquals('Nachricht 1', $translator->translate('Message 1', 'default', 'de_DE'));
 }
示例#4
0
 public function testTranslate()
 {
     $loader = new TestLoader();
     $loader->textDomain = new TextDomain(array('foo' => 'bar'));
     $this->translator->getPluginManager()->setService('test', $loader);
     $this->translator->addTranslationFile('test', null);
     $this->assertEquals('bar', $this->translator->translate('foo'));
 }
示例#5
0
 public function addErrorMessage($message)
 {
     if ($this->translator) {
         $namespace = explode('\\', get_class($this))[0];
         $message = $this->translator->translate($message, $namespace);
     }
     $this->errorMessages[] = $message;
 }
示例#6
0
 /**
  * Get One Element By Id
  * @param $id
  * @return string
  */
 public function getElementById($id)
 {
     if (!isset($this->list[$id])) {
         return null;
     }
     $element = $this->list[$id];
     $translatedElement = $this->translator->translate($element, 'GdproList');
     return $translatedElement;
 }
示例#7
0
 public function testTranslateWithCache()
 {
     $cache = \Zend\Cache\StorageFactory::factory(array('adapter' => 'memory'));
     $this->translator->setCache($cache);
     $loader = new TestLoader();
     $loader->textDomain = new TextDomain(array('foo' => 'bar'));
     $this->translator->getPluginManager()->setService('test', $loader);
     $this->translator->addTranslationFile('test', null);
     $this->assertEquals('bar', $this->translator->translate('foo'));
 }
示例#8
0
 /**
  * @param $str
  * @param pt_BR $locale
  * @return string
  */
 public function __invoke($str, $locale = 'pt_BR', $typeTranslate = 'PhpArray', $pathFileTranslate = null)
 {
     $translator = new Translator();
     if (!empty($pathFileTranslate)) {
         $translator->addTranslationFile($typeTranslate, $pathFileTranslate, 'default', $locale);
     }
     return $translator->translate($str, $locale);
 }
示例#9
0
 /**
  * Translate the route texts
  *
  * @param array $routes
  *
  * @return array
  */
 protected function translateRoutes(array $routes = array())
 {
     foreach ($routes as $routeKey => $routeParams) {
         if (isset($routeParams['description'])) {
             $routes[$routeKey]['description'] = $this->translator->translate($routeParams['description']);
         }
         if (isset($routeParams['short_description'])) {
             $routes[$routeKey]['short_description'] = $this->translator->translate($routeParams['short_description']);
         }
         if (isset($routeParams['options_descriptions'])) {
             foreach ($routeParams['options_descriptions'] as $optionKey => $optionText) {
                 $routes[$routeKey]['options_descriptions'][$optionKey] = $this->translator->translate($optionText);
             }
         }
     }
     return $routes;
 }
示例#10
0
 /**
  * Sets custom messages to filter input validators.
  * If provided $validators[], it will be filled with validator instances to be attached further on to a filter input,
  * otherwise the validator messages of the particular filter input provided will be amended accordingly.
  *
  * @param InputInterface $input
  * @param null|callback $inputLabel
  * @param null|array $validators
  */
 public function setValidatorMessages(InputInterface $input, $inputLabel = null, &$validators = null)
 {
     if (!is_null($validators) && !is_array($validators)) {
         throw new \InvalidArgumentException();
     }
     $inputLabel = is_callable($inputLabel) ? $inputLabel() : '';
     foreach ($input->getValidatorChain()->getValidators() as $validator) {
         //set message to validator StringLength
         if (isset($validator['instance']) && $validator['instance'] instanceof Validator\StringLength) {
             $validatorInstance = is_null($validators) ? $validator['instance'] : clone $validator['instance'];
             $validatorInstance->setMessage(sprintf($this->translator->translate('The input %s is more than %%max%% characters long'), $this->translator->translate($inputLabel)), Validator\StringLength::TOO_LONG);
             if (is_array($validators)) {
                 $validators[] = $validatorInstance;
             }
         }
     }
 }
示例#11
0
 public function translate($message, $textDomain = 'default', $locale = null)
 {
     if ($textDomain != 'default') {
         $this->setTextDomain($textDomain);
     }
     if ($locale != null) {
         $this->setLocale($locale);
     }
     return parent::translate($message, $this->textDomain, $this->locale);
 }
示例#12
0
 /**
  * @param string $queue_name
  * @param string $to
  * @param string $subject
  * @param string $body
  * @param null $from
  * @param true $html
  */
 public function addMail($queue_name, $to, $subject, $body, $from = null, $html = false)
 {
     $message = new \Zend\Mail\Message();
     $message->setTo($to);
     $message->setSubject($this->translate ? $this->translate->translate($subject) : $subject);
     if ($html) {
         $bodyPart = new \Zend\Mime\Message();
         $bodyMessage = new \Zend\Mime\Part($body);
         $bodyMessage->type = 'text/html';
         $bodyPart->setParts(array($bodyMessage));
         $message->setBody($bodyPart);
     } else {
         $message->setBody($body);
     }
     $message->setEncoding("UTF-8");
     if ($from) {
         $message->setFrom($from);
     } else {
         $message->setFrom($this->config['default_from']);
     }
     $this->table->add($queue_name, $message);
 }
示例#13
0
 public function testTranslationsAreStoredInCache()
 {
     $cache = \Zend\Cache\StorageFactory::factory(array('adapter' => 'memory'));
     $this->translator->setCache($cache);
     $loader = new TestLoader();
     $loader->textDomain = new TextDomain(array('foo' => 'bar'));
     $this->translator->getPluginManager()->setService('test', $loader);
     $this->translator->addTranslationFile('test', null);
     $this->assertEquals('bar', $this->translator->translate('foo'));
     $item = $cache->getItem('Zend_I18n_Translator_Messages_' . md5('default' . 'en_EN'));
     $this->assertInstanceOf('Zend\\I18n\\Translator\\TextDomain', $item);
     $this->assertEquals('bar', $item['foo']);
 }
示例#14
0
 public function testListenerOnNoMessagesLoadedEventCanReturnTextDomainObject()
 {
     $trigger = null;
     $doNotTrigger = null;
     $textDomain = new TextDomain(array('foo' => 'BOOYAH'));
     $this->translator->enableEventManager();
     $events = $this->translator->getEventManager();
     $events->attach(Translator::EVENT_NO_MESSAGES_LOADED, function (EventInterface $event) use(&$trigger) {
         $trigger = true;
     });
     $events->attach(Translator::EVENT_NO_MESSAGES_LOADED, function (EventInterface $event) use($textDomain) {
         return $textDomain;
     });
     $events->attach(Translator::EVENT_NO_MESSAGES_LOADED, function (EventInterface $event) use(&$doNotTrigger) {
         $doNotTrigger = true;
     });
     $result = $this->translator->translate('foo', 'bar', 'baz');
     $this->assertTrue($trigger);
     $this->assertNull($doNotTrigger);
     $this->assertEquals('BOOYAH', $result);
 }
示例#15
0
 /**
  * Translate a string if a translator is provided.
  *
  * @param string $msg Message to translate
  *
  * @return string
  */
 public function translate($msg)
 {
     return null !== $this->translator ? $this->translator->translate($msg) : $msg;
 }
示例#16
0
 protected function __($text, $parameters = array())
 {
     return vsprintf($this->translator->translate($text), $parameters);
 }
 /**
  * Constructor
  * @param \BoilerAppAccessControl\Repository\AuthAccessRepository $oAuthAccessRepository
  * @param \Zend\I18n\Translator\Translator $oTranslator
  */
 public function __construct(\BoilerAppAccessControl\Repository\AuthAccessRepository $oAuthAccessRepository, \Zend\I18n\Translator\Translator $oTranslator)
 {
     $this->add(array('name' => 'new_auth_access_username_identity', 'required' => true, 'filters' => array(array('name' => 'StringTrim')), 'validators' => array(array('name' => 'BoilerAppAccessControl\\Validator\\NoSpacesValidator', 'break_chain_on_failure' => true), array('name' => 'stringLength', 'options' => array('max' => 255), 'break_chain_on_failure' => true), array('name' => 'BoilerAppAccessControl\\Validator\\IdentityAvailabilityValidator', 'options' => array('identityName' => $oTranslator->translate('the_username'), 'checkAvailabilityCallback' => array($oAuthAccessRepository, 'isUsernameIdentityAvailable'))))));
 }
示例#18
0
 /**
  * @param   string  $key
  * @return  string
  */
 public function __invoke($key)
 {
     return $this->translator->translate($this->prefix . $key . $this->postfix, $this->textDomain, $this->locale);
 }
示例#19
0
 /**
  * Translate a message.
  *
  * @param  string $message
  * @param  string $textDomain
  * @param  string $locale
  * @return string
  */
 public function translate($message, $textDomain = 'default', $locale = null)
 {
     $messageArray = array();
     if (is_array($message)) {
         $messageArray = $message;
         $message = array_shift($messageArray);
     }
     if (true === $this->scaffold) {
         $this->scaffold($message);
     }
     $trMessage = strtolower($message);
     $trdMessage = parent::translate($trMessage, $textDomain, $locale);
     if ($trMessage == $trdMessage) {
         if ($messageArray) {
             return $this->translateTemplate($message, $messageArray);
         } else {
             return $message;
         }
     }
     if ($messageArray) {
         return $this->translateTemplate($trdMessage, $messageArray);
     }
     return $trdMessage;
 }
示例#20
0
 /**
  * Translate a message with specific domain and locale
  *
  * {@inheritDoc}
  */
 public function translate($message, $textDomain = null, $locale = null)
 {
     if (null === $textDomain) {
         $textDomain = $this->getTextDomain();
     }
     return parent::translate($message, $textDomain, $locale);
 }
示例#21
0
 /**
  * Write a list item line for third level
  *
  * @param string $message
  * @param array  $placeholders
  */
 public function writeListItemLineLevel3($message, array $placeholders = array())
 {
     $this->adapter->write('           * ');
     $this->adapter->writeLine(vsprintf($this->translator->translate($message), $placeholders));
 }
示例#22
0
 /**
  * Attempts to translate the given message or code into the provided locale.
  *
  * @param string $message    The message or code to translate.
  * @param string $textDomain A message may be located in a domain, here you can provide in which.
  * @param null   $locale     The locale to translate to or the default if not set.
  *
  * @return string
  */
 public function translate($message, $textDomain = self::DEFAULT_DOMAIN, $locale = null)
 {
     return parent::translate($message, $textDomain, $locale);
 }
 public function testTranslate()
 {
     $message = 'foo bar';
     $this->assertEquals($this->translator->translate($message), $this->extension->translate($message));
 }
示例#24
0
 public function translate($message, $textDomain = null, $locale = null)
 {
     return $this->translator->translate($message, $textDomain, $locale);
 }
示例#25
0
 /**
  * Translate a plural message.
  *
  * @param   string      $singular
  * @param   string      $plural
  * @param   int         $number
  * @param   string      $textDomain
  * @param   string|null $locale
  * @return  string
  * @throws  Exception\OutOfBoundsException
  */
 public function translatePlural($singular, $plural, $number, $textDomain = 'default', $locale = null)
 {
     $locale = (string) $locale ?: $this->getLocale();
     $textDomain = strstr($singular, '.', true) ?: strstr($plural, '.', true) ?: $textDomain;
     $translation = $this->getTranslatedMessage($singular, $locale, $textDomain);
     if (is_array($translation) || $translation instanceof ArrayAccess) {
         if (isset($this->messages[$textDomain][$locale])) {
             return parent::translatePlural($singular, $plural, $number, $textDomain, $locale);
         }
         $index = $this->myMessages[$textDomain][$locale]->getPluralRule()->evaluate($number);
         if (!isset($translation[$index])) {
             throw new Exception\OutOfBoundsException(sprintf('Provided index %d does not exist in plural array', $index));
         }
         return $translation[$index];
     }
     return parent::translate(1 == $number ? $singular : $plural, $textDomain, $locale);
 }
示例#26
0
 /**
  * Process a single value
  *
  * @param $value
  * @return mixed
  */
 public function processValue($value)
 {
     return $this->translator->translate($value, $this->textDomain, $this->locale);
 }
示例#27
0
 protected function translatePart($translate, $result, $textDomain, $locale)
 {
     $newResult = parent::translate($result, $textDomain, $locale);
     $translate = str_replace('{{' . $result . '}}', $newResult, $translate);
     return $translate;
 }