/** * 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); }
public function translate($text) { if ($this->translator === null) { $this->translator = $this->getApplicationServiceLocator()->get('translator'); } return $this->translator->translate($text); }
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')); }
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')); }
public function addErrorMessage($message) { if ($this->translator) { $namespace = explode('\\', get_class($this))[0]; $message = $this->translator->translate($message, $namespace); } $this->errorMessages[] = $message; }
/** * 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; }
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')); }
/** * @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); }
/** * 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; }
/** * 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; } } } }
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); }
/** * @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); }
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']); }
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); }
/** * 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; }
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')))))); }
/** * @param string $key * @return string */ public function __invoke($key) { return $this->translator->translate($this->prefix . $key . $this->postfix, $this->textDomain, $this->locale); }
/** * 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; }
/** * 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); }
/** * 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)); }
/** * 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)); }
public function translate($message, $textDomain = null, $locale = null) { return $this->translator->translate($message, $textDomain, $locale); }
/** * 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); }
/** * Process a single value * * @param $value * @return mixed */ public function processValue($value) { return $this->translator->translate($value, $this->textDomain, $this->locale); }
protected function translatePart($translate, $result, $textDomain, $locale) { $newResult = parent::translate($result, $textDomain, $locale); $translate = str_replace('{{' . $result . '}}', $newResult, $translate); return $translate; }