コード例 #1
0
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $adapter = null;
     $locale = null;
     $config = $serviceLocator->has('Config') ? $serviceLocator->get('Config') : [];
     $config = isset($config['locale_manager']) ? $config['locale_manager'] : [];
     if ($serviceLocator->has('LocaleManager\\Adapter\\AdapterInterface')) {
         $adapter = $serviceLocator->get('LocaleManager\\Adapter\\AdapterInterface');
         if (!$adapter instanceof AdapterInterface) {
             throw new ServiceNotCreatedException(sprintf('LocaleManager requires that the %s service implement %s; received "%s"', 'LocaleManager\\Adapter\\AdapterInterface', 'LocaleManager\\Adapter\\AdapterInterface', is_object($adapter) ? get_class($adapter) : gettype($adapter)));
         }
     }
     $manager = new LocaleManager($adapter);
     // Set the current locale
     if (isset($config['locale'])) {
         $locale = $config['locale'];
     } elseif ($serviceLocator->has('Translator')) {
         $translator = $serviceLocator->get('Translator');
         if ($translator instanceof \Zend\Mvc\I18n\Translator) {
             $translator = $translator->getTranslator();
         }
         if (method_exists($translator, 'getLocale')) {
             $locale = $translator->getLocale();
         }
     }
     if ($locale === null) {
         $locale = \Locale::getDefault();
     }
     $manager->setLocale($locale);
     // Return the manager
     return $manager;
 }
コード例 #2
0
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $config = $serviceLocator->has('Config') ? $serviceLocator->get('Config') : array();
     $config = isset($config['locale_manager']) ? $config['locale_manager'] : array();
     $localeManager = new LocaleManager($config);
     $localeManager->setServiceManager($serviceLocator);
     if ($localeManager instanceof EventManagerAwareInterface) {
         $localeManager->setEventManager($serviceLocator->get('Application')->getEventManager());
     }
     if ($serviceLocator->has('Application')) {
         $serviceLocator->get('Application')->getEventManager()->attach(new LocaleManagerListener());
     }
     // --- Start Router Config ---
     $router = $serviceLocator->get('Router');
     if ($router instanceof LocaleManagerAwareInterface) {
         $router->setLocaleManager($localeManager);
         // TODO: Set the detection method for the router
     }
     if ($router instanceof TranslatorAwareInterface) {
         // Try setting a translator
         if (!$router->hasTranslator()) {
             // Inject the translator
             if ($serviceLocator->has('MvcTranslator')) {
                 $router->setTranslator($serviceLocator->get('MvcTranslator'));
             } elseif ($serviceLocator->has('Zend\\I18n\\Translator\\TranslatorInterface')) {
                 $router->setTranslator($serviceLocator->get('Zend\\I18n\\Translator\\TranslatorInterface'));
             } elseif ($serviceLocator->has('Translator')) {
                 $router->setTranslator($serviceLocator->get('Translator'));
             } else {
                 if (!extension_loaded('intl')) {
                     $router->setTranslator(new MvcTranslator(new DummyTranslator()));
                 } else {
                     // For BC purposes (pre-2.3.0), use the I18n Translator
                     $router->setTranslator(new MvcTranslator(new Translator()));
                 }
             }
         }
         // Set the router text domain
         $router->setTranslatorTextDomain('router');
     }
     // --- End Router Config ---
     // Return the instance
     return $localeManager;
 }
コード例 #3
0
 /**
  * Returns current runtime locale.
  *
  * @return string
  */
 public function __invoke()
 {
     $locale = $this->localeManager->getLocale();
     $locale = str_replace('_', '-', $locale);
     return $locale;
 }
コード例 #4
0
 /**
  * assemble(): defined by \Zend\Mvc\Router\RouteInterface interface.
  *
  * @see    \Zend\Mvc\Router\RouteInterface::assemble()
  * @param  array $params
  * @param  array $options
  * @return mixed
  * @throws Exception\InvalidArgumentException
  * @throws Exception\RuntimeException
  */
 public function assemble(array $params = array(), array $options = array())
 {
     if (!isset($options['name'])) {
         throw new Exception\InvalidArgumentException('Missing "name" option');
     }
     $names = explode('/', $options['name'], 2);
     $route = $this->routes->get($names[0]);
     if (!$route) {
         throw new Exception\RuntimeException(sprintf('Route with name "%s" not found', $names[0]));
     }
     if (isset($names[1])) {
         if (!$route instanceof TreeRouteStack) {
             //die("DEBUG: route is " . gettype($route));
             throw new Exception\RuntimeException(sprintf('Route with name "%s" does not have child routes', $names[0]));
         }
         $options['name'] = $names[1];
     } else {
         unset($options['name']);
     }
     // Translator options
     if ($this->hasTranslator() && $this->isTranslatorEnabled()) {
         $options['translator'] = isset($options['translator']) ? $options['translator'] : $this->getTranslator();
         $options['text_domain'] = isset($options['text_domain']) ? $options['text_domain'] : $this->getTranslatorTextDomain();
         $options['locale'] = isset($options['locale']) ? $options['locale'] : str_replace('-', '_', $this->localeManager->getLocale());
     }
     if (isset($options['only_return_path']) && $options['only_return_path']) {
         return $this->getBaseUrlWithLocale($options['locale']) . $route->assemble(array_merge($this->defaultParams, $params), $options);
     }
     if (!isset($options['uri'])) {
         $uri = new HttpUri();
         if (isset($options['force_canonical']) && $options['force_canonical']) {
             if ($this->requestUri === null) {
                 throw new Exception\RuntimeException('Request URI has not been set');
             }
             $uri->setScheme($this->requestUri->getScheme())->setHost($this->requestUri->getHost())->setPort($this->requestUri->getPort());
         }
         $options['uri'] = $uri;
     } else {
         $uri = $options['uri'];
     }
     $path = $this->getBaseUrlWithLocale($options['locale']) . $route->assemble(array_merge($this->defaultParams, $params), $options);
     if (isset($options['query'])) {
         $uri->setQuery($options['query']);
     }
     if (isset($options['fragment'])) {
         $uri->setFragment($options['fragment']);
     }
     if (isset($options['force_canonical']) && $options['force_canonical'] || $uri->getHost() !== null || $uri->getScheme() !== null) {
         if (($uri->getHost() === null || $uri->getScheme() === null) && $this->requestUri === null) {
             throw new Exception\RuntimeException('Request URI has not been set');
         }
         if ($uri->getHost() === null) {
             $uri->setHost($this->requestUri->getHost());
         }
         if ($uri->getScheme() === null) {
             $uri->setScheme($this->requestUri->getScheme());
         }
         return $uri->setPath($path)->normalize()->toString();
     } elseif (!$uri->isAbsolute() && $uri->isValidRelative()) {
         return $uri->setPath($path)->normalize()->toString();
     }
     return $path;
 }