Esempio n. 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);
 }
 public function init()
 {
     $this->app->configs->setDefaultConfig($this->id, new TranslatorConfig(), $this);
     $this->translator = $translator = new Translator();
     /** @var TranslatorConfig $config */
     $this->moduleConfig = $config = $this->app->configs->getConfig($this->id);
     if (!$this->app->getConfig()->developmentMode) {
         $c = new Filesystem();
         $o = new FilesystemOptions();
         $o->setCacheDir($this->app->cacheStorage->createStorage($this->id));
         $c->setOptions($o);
         $c->addPlugin(new Serializer());
         $translator->setCache($c);
     }
     $translator->setLocale($config->defaultLanguage);
     $folder = $this->app->parseUri($config->translationsDirectory);
     foreach ($config->contexts as $context => $file) {
         if (is_int($context)) {
             $context = $file;
             $file .= '.mo';
         }
         $translator->addTranslationFilePattern('gettext', $folder, '%s/' . $file, $context);
     }
     $this->translator = $translator;
 }
Esempio n. 3
0
 /**
  * Translate a plural message.
  *
  * @param  string  $singular
  * @param  string  $plural
  * @param  integer $number
  * @param  string  $textDomain
  * @param  string  $locale
  * @return string
  * @throws Exception\RuntimeException
  */
 public function __invoke($singular, $plural, $number, $textDomain = 'default', $locale = null)
 {
     if ($this->translator === null) {
         throw new Exception\RuntimeException('Translator has not been set');
     }
     return $this->translator->translatePlural($singular, $plural, $number, $textDomain, $locale);
 }
Esempio n. 4
0
 public function translatePlural($singular, $plural, $number, $textDomain, $locale)
 {
     if ($this->translator === null) {
         $this->translator = $this->getApplicationServiceLocator()->get('translator');
     }
     return $this->translator->translatePlural($singular, $plural, $number, $textDomain, $locale);
 }
 public function testLanguage()
 {
     $this->assertEquals('en', $this->translator->getLocale());
     $this->middleware->__invoke($this->request, new Response(), function ($req, $resp) {
         return $resp;
     });
     $this->assertEquals('es', $this->translator->getLocale());
 }
Esempio n. 6
0
 public function addErrorMessage($message)
 {
     if ($this->translator) {
         $namespace = explode('\\', get_class($this))[0];
         $message = $this->translator->translate($message, $namespace);
     }
     $this->errorMessages[] = $message;
 }
Esempio n. 7
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'));
 }
 public function setUp()
 {
     $this->testFilesDir = __DIR__ . '/../_files';
     $this->translator = new Translator();
     $this->translator->addTranslationFile('phparray', $this->testFilesDir . '/tokens.en.php', 'route', 'en');
     $this->translator->addTranslationFile('phparray', $this->testFilesDir . '/tokens.de.php', 'route', 'de');
     $this->fooRoute = ['type' => 'Segment', 'options' => ['route' => '/:locale'], 'child_routes' => ['index' => ['type' => 'Segment', 'options' => ['route' => '/{homepage}']]]];
 }
Esempio n. 9
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);
 }
 /**
  * Get translator object.
  *
  * @return \Zend\I18n\Translator\Translator
  */
 public function getTranslator()
 {
     if (null === $this->translator) {
         $this->translator = clone $this->getServiceLocator()->get('Translator');
         $this->translator->addTranslationFile('ExtendedIni', APPLICATION_PATH . '/languages/native.ini', 'default', 'native');
         $this->translator->setLocale('native');
     }
     return $this->translator;
 }
Esempio n. 11
0
 /**
  * Process an incoming request and/or response.
  *
  * Accepts a server-side request and a response instance, and does
  * something with them.
  *
  * If the response is not complete and/or further processing would not
  * interfere with the work done in the middleware, or if the middleware
  * wants to delegate to another process, it can use the `$out` callable
  * if present.
  *
  * If the middleware does not return a value, execution of the current
  * request is considered complete, and the response instance provided will
  * be considered the response to return.
  *
  * Alternately, the middleware may return a response instance.
  *
  * Often, middleware will `return $out();`, with the assumption that a
  * later middleware will return a response.
  *
  * @param Request $request
  * @param Response $response
  * @param null|callable $out
  * @return null|Response
  */
 public function __invoke(Request $request, Response $response, callable $out = null)
 {
     if (!$request->hasHeader('Accept-Language')) {
         return $out($request, $response);
     }
     $locale = $request->getHeaderLine('Accept-Language');
     $this->translator->setLocale($this->normalizeLocale($locale));
     return $out($request, $response);
 }
Esempio n. 12
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;
 }
Esempio n. 13
0
 public function testTranslatePlurals()
 {
     $this->translator->setLocale('en_EN');
     $this->translator->addTranslationFile('phparray', $this->testFilesDir . '/translation_en.php', 'default', 'en_EN');
     $pl0 = $this->translator->translatePlural('Message 5', 'Message 5 Plural', 1);
     $pl1 = $this->translator->translatePlural('Message 5', 'Message 5 Plural', 2);
     $pl2 = $this->translator->translatePlural('Message 5', 'Message 5 Plural', 10);
     $this->assertEquals('Message 5 (en) Plural 0', $pl0);
     $this->assertEquals('Message 5 (en) Plural 1', $pl1);
     $this->assertEquals('Message 5 (en) Plural 2', $pl2);
 }
Esempio n. 14
0
 public function onBootstrap(MvcEvent $e)
 {
     $eventManager = $e->getApplication()->getEventManager();
     $moduleRouteListener = new ModuleRouteListener();
     $moduleRouteListener->attach($eventManager);
     $eventManager->attach(MvcEvent::EVENT_ROUTE, [$this, 'protectPage'], -100);
     $t = new I18nTranslator();
     $t->setLocale('de_DE');
     $translator = new Translator($t);
     $translator->addTranslationFile('phpArray', 'vendor/zendframework/zend-i18n-resources/languages/de/Zend_Validate.php', 'default', 'de_DE');
     AbstractValidator::setDefaultTranslator($translator);
 }
 /**
  * {@inheritDoc}
  */
 public function createTranslatorAdapter($locale)
 {
     $cache = new ZendCacheDriver('cache/expensive');
     $t = new Translator();
     $t->setCache($cache);
     $adapter = new TranslatorAdapter($t);
     $adapter->setLocale($locale);
     if (isset($this->translationLoaderRepository)) {
         foreach ($this->translationLoaderRepository->getTranslationLoaders() as $key => $loader) {
             $loader->loadTranslations($adapter);
         }
     }
     return $adapter;
 }
Esempio n. 16
0
 /**
  * @test
  */
 public function localeGetsNormalized()
 {
     $this->assertEquals('ru', $this->translator->getLocale());
     $request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'es_ES');
     $this->middleware->__invoke($request, new Response(), function ($req, $resp) {
         return $resp;
     });
     $this->assertEquals('es', $this->translator->getLocale());
     $request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'en-US');
     $this->middleware->__invoke($request, new Response(), function ($req, $resp) {
         return $resp;
     });
     $this->assertEquals('en', $this->translator->getLocale());
 }
 public function setUp()
 {
     $command = new GenerateCharsetCommand(Translator::factory([]));
     $app = new Application();
     $app->add($command);
     $this->commandTester = new CommandTester($command);
 }
 /**
  * @param ServiceLocatorInterface $serviceLocator
  * @return MvcTranslator
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     // Assume that if a user has registered a service for the
     // TranslatorInterface, it must be valid
     if ($serviceLocator->has('Zend\\I18n\\Translator\\TranslatorInterface')) {
         return new MvcTranslator($serviceLocator->get('Zend\\I18n\\Translator\\TranslatorInterface'));
     }
     // If ext/intl is not loaded, return a dummy translator
     if (!extension_loaded('intl')) {
         return new MvcTranslator(new DummyTranslator());
     }
     // Load a translator from configuration, if possible
     if ($serviceLocator->has('Config')) {
         $config = $serviceLocator->get('Config');
         // 'translator' => false
         if (array_key_exists('translator', $config) && $config['translator'] === false) {
             return new MvcTranslator(new DummyTranslator());
         }
         // 'translator' => array( ... translator options ... )
         if (array_key_exists('translator', $config) && (is_array($config['translator']) && !empty($config['translator']) || $config['translator'] instanceof Traversable)) {
             $i18nTranslator = Translator::factory($config['translator']);
             $serviceLocator->setService('Zend\\I18n\\Translator\\TranslatorInterface', $i18nTranslator);
             return new MvcTranslator($i18nTranslator);
         }
     }
     // For BC purposes (pre-2.3.0), use the I18n Translator
     return new MvcTranslator(new Translator());
 }
 public function setUp()
 {
     $this->factory = new RendererFactory();
     $this->container = $this->prophesize(ContainerInterface::class);
     $this->container->get('translator')->willReturn(Translator::factory([]));
     $this->container->get('config')->willReturn(['navigation' => [], 'recaptcha' => []]);
     $this->container->get(RouteAssembler::class)->willReturn($this->prophesize(RouteAssembler::class)->reveal());
 }
 public function setUp()
 {
     $this->urlShortener = $this->prophesize(UrlShortener::class);
     $command = new GenerateShortcodeCommand($this->urlShortener->reveal(), Translator::factory([]), ['schema' => 'http', 'hostname' => 'foo.com']);
     $app = new Application();
     $app->add($command);
     $this->commandTester = new CommandTester($command);
 }
 /**
  * Create a Translator instance.
  *
  * @param ContainerInterface $container
  * @param string $requestedName
  * @param null|array $options
  * @return Translator
  */
 public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
 {
     // Configure the translator
     $config = $container->get('config');
     $trConfig = isset($config['translator']) ? $config['translator'] : [];
     $translator = Translator::factory($trConfig);
     return $translator;
 }
Esempio n. 22
0
 public function setUp()
 {
     $this->visitsTracker = $this->prophesize(VisitsTrackerInterface::class);
     $command = new GetVisitsCommand($this->visitsTracker->reveal(), Translator::factory([]));
     $app = new Application();
     $app->add($command);
     $this->commandTester = new CommandTester($command);
 }
Esempio n. 23
0
 public function setUp()
 {
     $this->apiKeyService = $this->prophesize(ApiKeyService::class);
     $command = new ListKeysCommand($this->apiKeyService->reveal(), Translator::factory([]));
     $app = new Application();
     $app->add($command);
     $this->commandTester = new CommandTester($command);
 }
 public function testInvoke()
 {
     $sm = new ServiceManager();
     $sm->setService(Translator::class, Translator::factory([]));
     $sm->setService(RouterInterface::class, $this->prophesize(RouterInterface::class)->reveal());
     $instance = $this->factory->__invoke($sm);
     $this->assertInstanceOf(LanguageMiddleware::class, $instance);
 }
Esempio n. 25
0
 public function setUp()
 {
     $this->urlShortener = $this->prophesize(UrlShortener::class);
     $command = new ResolveUrlCommand($this->urlShortener->reveal(), Translator::factory([]));
     $app = new Application();
     $app->add($command);
     $this->commandTester = new CommandTester($command);
 }
Esempio n. 26
0
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     // Configure the translator
     $config = $serviceLocator->get('Configuration');
     $trConfig = isset($config['translator']) ? $config['translator'] : array();
     $translator = Translator::factory($trConfig);
     return $translator;
 }
Esempio n. 27
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;
 }
Esempio n. 28
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;
             }
         }
     }
 }
 public function setUp()
 {
     $this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class);
     $app = new Application();
     $command = new ListShortcodesCommand($this->shortUrlService->reveal(), Translator::factory([]));
     $app->add($command);
     $this->questionHelper = $command->getHelper('question');
     $this->commandTester = new CommandTester($command);
 }
 public function setUp()
 {
     $routeAssemblerProphezy = $this->prophesize(RouteAssembler::class);
     $routeAssemblerProphezy->assembleUrl('home', true)->willReturn('/foo/bar');
     $routeAssemblerProphezy->assembleUrl(null, ['lang' => 'es'])->willReturn('/es');
     $routeAssemblerProphezy->assembleUrl(null, ['lang' => 'en'])->willReturn('/en');
     $routeAssemblerProphezy->getCurrentRouteResult()->willReturn(RouteResult::fromRouteMatch('home', 'HelloWorld', []));
     $this->extension = new NavigationExtension(Translator::factory([]), $routeAssemblerProphezy->reveal(), ['menu' => [['route' => 'home', 'icon' => 'house', 'label' => 'Home'], ['uri' => 'http://foo.com', 'target' => true, 'label' => 'Blog', 'icon' => 'book']], 'lang_menu' => [['label' => 'Español', 'class' => 'es', 'params' => ['lang' => 'es']], ['label' => 'English', 'class' => 'en', 'params' => ['lang' => 'en']]]]);
 }