/** * Forcing the request locale if the user entity uses a custom locale that is set on the entity * * @param InteractiveLoginEvent $event */ public function onLogin(InteractiveLoginEvent $event) { $user = $event->getAuthenticationToken()->getUser(); // Skipping login that is not coming from the backend User entity if (false == $user instanceof User) { return; } if ($locale = $user->getLocale()) { $event->getRequest()->setLocale($locale); } // Redirection on the first application to which the User has access $appRepo = $this->doctrine->getRepository('UnifikSystemBundle:App'); $apps = $appRepo->findAllHasAccess($this->securityContext, $user->getId()); if ($apps && !in_array($apps[0]->getId(), array(AppRepository::FRONTEND_APP_ID, AppRepository::BACKEND_APP_ID))) { $event->getRequest()->request->set('_target_path', $this->router->generate('unifik_system_backend_switch_managed_app', array('appSlug' => $apps[0]->getSlug()), UrlGeneratorInterface::ABSOLUTE_URL)); } }
/** * Setting lang seo alternatives * * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { $langs = ['en', 'ru']; $request = $event->getRequest(); $route = $request->attributes->get('_route'); if ($route) { $routeParams = $request->attributes->get('_route_params'); $attributes = $request->query->all(); if (!is_null($routeParams)) { $attributes = array_merge($attributes, $routeParams); } foreach ($langs as $lang) { $attributes['_locale'] = $lang; $this->seoPage->addLangAlternate($this->router->generate($route, $attributes, true), $lang); } } }
public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); $route = $request->get('_route'); $locale = $request->get('_locale'); $route_params = $request->attributes->get('_route_params'); //menu locale switcher used? if ($request->query->has('force-locale-switch')) { $request->cookies->set('hl', $locale); $url = $this->router->generate($route, ['_locale' => $locale] + $route_params); $event->setResponse(new RedirectResponse($url)); return; } //first request the cookie is empty. Gets auto set by I18nRouter if ($request->cookies->get('hl')) { return; } $preferredLocale = $request->getPreferredLanguage($this->availableLocals); if ($preferredLocale && $request->attributes->get('_locale') != $preferredLocale) { $url = $this->router->generate($route, ['_locale' => $preferredLocale] + $route_params); $event->setResponse(new RedirectResponse($url)); } }
/** * Fallback * * Fallback to the parent if it exists or to the current Section in the Core * * @param $element * @param $locale * * @return mixed */ protected function fallBack($element, $locale) { if ($parent = $element->getParent()) { $this->setElement($parent); $data = $this->generate(); $url = $data[$locale->getCode()]['url']; } elseif (false == $element instanceof Section) { $this->setElement($this->core->getSection()); $data = $this->generate(); $url = $data[$locale->getCode()]['url']; } else { // Fallback to the homepage try { $url = $this->router->generate('section_id_1', array('_locale' => $locale->getCode())); } catch (\Exception $e) { $url = ''; } } return $url; }
/** * Gets the translator required for checking the DoubleLocale tests (en_UK etc) */ private function getNonRedirectingHostMapRouter($config = 'routing.yml') { $container = new Container(); $container->set('routing.loader', new YamlFileLoader(new FileLocator(__DIR__ . '/Fixture'))); $translator = new Translator('en_UK', new MessageSelector()); $translator->setFallbackLocales(array('en')); $translator->addLoader('yml', new TranslationLoader()); $translator->addResource('yml', __DIR__ . '/Fixture/routes.en_UK.yml', 'en_UK', 'routes'); $translator->addResource('yml', __DIR__ . '/Fixture/routes.en_US.yml', 'en_US', 'routes'); $translator->addResource('yml', __DIR__ . '/Fixture/routes.nl.yml', 'nl', 'routes'); $translator->addResource('yml', __DIR__ . '/Fixture/routes.en.yml', 'en', 'routes'); $container->set('i18n_loader', new I18nLoader(new DefaultRouteExclusionStrategy(), new DefaultPatternGenerationStrategy('custom', $translator, array('en_UK', 'en_US', 'nl_NL', 'nl_BE'), sys_get_temp_dir(), 'routes', 'en_UK'))); $router = new I18nRouter($container, $config); $router->setRedirectToHost(false); $router->setI18nLoaderId('i18n_loader'); $router->setDefaultLocale('en_UK'); $router->setHostMap(array('en_UK' => 'uk.test', 'en_US' => 'us.test', 'nl_NL' => 'nl.test', 'nl_BE' => 'be.test')); return $router; }
private function getRouter($config = 'routing.yml', $translator = null) { $container = new Container(); $container->set('routing.loader', new YamlFileLoader(new FileLocator(__DIR__ . '/Fixture'))); if (null === $translator) { $translator = new Translator('en', new MessageSelector()); $translator->setFallbackLocale('en'); $translator->addLoader('yml', new TranslationLoader()); $translator->addResource('yml', file_get_contents(__DIR__ . '/Fixture/routes.de.yml'), 'de', 'routes'); $translator->addResource('yml', file_get_contents(__DIR__ . '/Fixture/routes.en.yml'), 'en', 'routes'); } $container->set('i18n_loader', new I18nLoader($translator, array('en', 'de', 'fr'), 'en', 'routes', 'custom', sys_get_temp_dir())); $router = new I18nRouter($container, $config); $router->setI18nLoaderId('i18n_loader'); $router->setDefaultLocale('en'); return $router; }