public function testIsLanguageSupported() { foreach (array_keys($this->languages) as $language) { $this->assertTrue($this->localeService->isLanguageSupported($language)); } $this->assertFalse($this->localeService->isLanguageSupported('nonExistent')); }
public function onDispatchError(MvcEvent $e) { if ($e->getRequest() instanceof \Zend\Console\Request || Application::ERROR_ROUTER_NO_MATCH != $e->getError()) { return; } $router = $e->getRouter(); $basePath = $router->getBaseUrl(); $match = []; if (preg_match('~^' . $basePath . '/([a-z]{2})(?:/|$)~', $e->getRequest()->getRequestUri(), $match)) { /* It seems we have already a language in the URI * Now there are two possibilities: * * 1: The Language is not supported * -> set translator locale to browser locale if supported * or default. Do not forget to set the appropriate route param 'lang' * * 2: Language is supported, but the rest of the route * does not match * -> set translator locale to provided language */ $lang = $this->localeService->isLanguageSupported($match[1]) ? $match[1] : $this->detectLanguage($e); $this->setLocale($e, $lang); return; } /* We have no language key in the URI * Let's prepend the browser language locale if supported or * the default to the URI. * * If a route matches this prepended URI, we do a redirect, * else we set the translator locale and let the event propagate * to the ROUTE_NO_MATCH error renderer. */ $request = clone $e->getRequest(); // clone the request, because maybe we $origUri = str_replace($basePath, '', $request->getRequestUri()); $lang = $this->detectLanguage($e); $langUri = rtrim("{$basePath}/{$lang}{$origUri}", '/'); if ($router->match($request->setUri($langUri)) instanceof RouteMatch) { $e->stopPropagation(true); //$e->setError(false); return $this->redirect($e->getResponse(), $langUri); } $this->setLocale($e, $lang); }