/**
  * Handles enabling translations for controllers that are not pages
  */
 public function onBeforeInit()
 {
     //Bail for the root url controller and model as controller classes as they handle this internally, also disable for development admin and cms
     if ($this->owner instanceof MultilingualRootURLController || $this->owner instanceof MultilingualModelAsController || $this->owner instanceof LeftAndMain || $this->owner instanceof DevelopmentAdmin || $this->owner instanceof TestRunner) {
         return;
     }
     //Bail for pages since this would have been handled by MultilingualModelAsController, we're assuming that data has not been set to a page by other code
     if (method_exists($this->owner, 'data') && $this->owner->data() instanceof SiteTree) {
         return;
     }
     //Check if the locale is in the url
     $request = $this->owner->getRequest();
     if ($request && $request->param('Language')) {
         $language = $request->param('Language');
         if (Config::inst()->get('MultilingualRootURLController', 'UseLocaleURL')) {
             $locale = $language;
         } else {
             if (strpos($request->param('Language'), '_') !== false) {
                 //Invalid format so redirect to the default
                 $url = $request->getURL(true);
                 $default = Config::inst()->get('MultilingualRootURLController', 'UseLocaleURL') ? Translatable::default_locale() : Translatable::default_lang();
                 $this->owner->redirect(preg_replace('/^' . preg_quote($language, '/') . '\\//', $default . '/', $url), 301);
                 return;
             } else {
                 $locale = i18n::get_locale_from_lang($language);
             }
         }
         if (in_array($locale, Translatable::get_allowed_locales())) {
             //Set the language cookie
             Cookie::set('language', $language);
             //Set the various locales
             Translatable::set_current_locale($locale);
             i18n::set_locale($locale);
         } else {
             //Unknown language so redirect to the default
             $url = $request->getURL(true);
             $default = Config::inst()->get('MultilingualRootURLController', 'UseLocaleURL') ? Translatable::default_locale() : Translatable::default_lang();
             $this->owner->redirect(preg_replace('/^' . preg_quote($language, '/') . '\\//', $default . '/', $url), 301);
         }
         return;
     }
     //Detect the locale
     if ($locale = MultilingualRootURLController::detect_browser_locale()) {
         if (Config::inst()->get('MultilingualRootURLController', 'UseLocaleURL')) {
             $language = $locale;
         } else {
             $language = i18n::get_lang_from_locale($locale);
         }
         //Set the language cookie
         Cookie::set('language', $language);
         //Set the various locales
         Translatable::set_current_locale($locale);
         i18n::set_locale($locale);
     }
 }
 public function tearDown()
 {
     MultilingualRootURLController::set_use_locale_url($this->origLocaleRoutingEnabled);
     Translatable::set_current_locale($this->origCurrentLocale);
     Translatable::set_default_locale($this->origLocale);
     Translatable::set_allowed_locales($this->origAllowedLocales);
     i18n::set_locale($this->origi18nLocale);
     Cookie::force_expiry('language');
     if ($this->origCookieLocale) {
         Cookie::set('language', $this->origCookieLocale);
     }
     $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $this->origAcceptLanguage;
     MultilingualRootURLController::reset();
     parent::tearDown();
 }
 /**
  * Tests to see if the french home page is the root url and the english home page is not for french browsers
  */
 public function testFrenchShouldBeRoot()
 {
     //Set accept language to french
     $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr-FR,fr;q=0.5';
     Translatable::set_default_locale('fr_FR');
     Translatable::set_current_locale('fr_FR');
     i18n::set_locale('fr_FR');
     $default = $this->objFromFixture('Page', 'home');
     $defaultFR = $this->objFromFixture('Page', 'home_fr');
     $this->assertEquals(false, MultilingualRootURLController::should_be_on_root($default));
     $this->assertEquals(true, MultilingualRootURLController::should_be_on_root($defaultFR));
 }
 /**
  * Verifies the correct home page is detected for the french locale
  */
 public function testFrenchGetHomepageLink()
 {
     //Set accept language to french
     $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr-FR,fr;q=0.5';
     Translatable::set_default_locale('fr_FR');
     Translatable::set_current_locale('fr_FR');
     i18n::set_locale('fr_FR');
     $this->assertEquals('maison', MultilingualRootURLController::get_homepage_link());
 }
 public function handleRequest(SS_HTTPRequest $request, DataModel $model = null)
 {
     self::$is_at_root = true;
     $this->setDataModel($model);
     $this->pushCurrent();
     $this->init();
     if ($language = $request->param('Language')) {
         if (Config::inst()->get('MultilingualRootURLController', 'UseLocaleURL')) {
             if (Config::inst()->get('MultilingualRootURLController', 'UseDashLocale')) {
                 //Language is missing a dash 404
                 if (strpos($language, '-') === false) {
                     //Locale not found 404
                     if ($response = ErrorPage::response_for(404)) {
                         return $response;
                     } else {
                         $this->httpError(404, 'The requested page could not be found.');
                     }
                     return $this->response;
                 }
                 $locale = explode('-', $language);
                 $locale[1] = strtoupper($locale[1]);
                 //Make sure that the language is all lowercase
                 if ($language == implode('-', $locale)) {
                     //Locale not found 404
                     if ($response = ErrorPage::response_for(404)) {
                         return $response;
                     } else {
                         $this->httpError(404, 'The requested page could not be found.');
                     }
                     return $this->response;
                 }
                 $locale = implode('_', $locale);
             } else {
                 $locale = $language;
             }
         } else {
             if (strpos($request->param('Language'), '_') !== false) {
                 //Locale not found 404
                 if ($response = ErrorPage::response_for(404)) {
                     return $response;
                 } else {
                     $this->httpError(404, 'The requested page could not be found.');
                 }
                 return $this->response;
             } else {
                 $locale = i18n::get_locale_from_lang($language);
             }
         }
         if (in_array($locale, Translatable::get_allowed_locales())) {
             Cookie::set('language', $language);
             Translatable::set_current_locale($locale);
             i18n::set_locale($locale);
             if (!DB::isActive() || !ClassInfo::hasTable('SiteTree')) {
                 $this->response = new SS_HTTPResponse();
                 $this->response->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null));
                 return $this->response;
             }
             $request->setUrl($language . '/' . self::get_homepage_link() . '/');
             $request->match('$Language/$URLSegment//$Action', true);
             $controller = new MultilingualModelAsController();
             $result = $controller->handleRequest($request, $model);
             $this->popCurrent();
             return $result;
         } else {
             //URL Param Locale is not allowed so redirect to default
             $this->redirect(Controller::join_links(Director::baseURL(), Config::inst()->get('MultilingualRootURLController', 'UseLocaleURL') ? Translatable::default_locale() : Translatable::default_lang()) . '/', 301);
             $this->popCurrent();
             return $this->response;
         }
     }
     //No Locale Param so detect browser language and redirect
     if ($locale = self::detect_browser_locale()) {
         if (Config::inst()->get('MultilingualRootURLController', 'UseLocaleURL')) {
             if (Config::inst()->get('MultilingualRootURLController', 'UseDashLocale')) {
                 $language = str_replace('_', '-', strtolower($locale));
             } else {
                 $language = $locale;
             }
         } else {
             $language = i18n::get_lang_from_locale($locale);
         }
         Cookie::set('language', $language);
         $this->redirect(Controller::join_links(Director::baseURL(), $language) . '/', 301);
         $this->popCurrent();
         return $this->response;
     }
     if (Config::inst()->get('MultilingualRootURLController', 'UseLocaleURL')) {
         if (Config::inst()->get('MultilingualRootURLController', 'UseDashLocale')) {
             $language = str_replace('_', '-', strtolower(Translatable::default_locale()));
         } else {
             $language = Translatable::default_locale();
         }
     } else {
         $language = Translatable::default_lang();
     }
     $this->redirect(Controller::join_links(Director::baseURL(), $language . '/'), 301);
     $this->popCurrent();
     return $this->response;
 }
 /**
  * @uses ModelAsController::getNestedController()
  * @return SS_HTTPResponse
  */
 public function handleRequest(SS_HTTPRequest $request, DataModel $model)
 {
     $this->request = $request;
     $this->setDataModel($model);
     $this->pushCurrent();
     //Get the local from the language param
     if (Config::inst()->get('MultilingualRootURLController', 'UseLocaleURL')) {
         if (Config::inst()->get('MultilingualRootURLController', 'UseDashLocale')) {
             //Language is missing a dash 404
             if (strpos($request->param('Language'), '-') === false) {
                 //Locale not found 404
                 if ($response = ErrorPage::response_for(404)) {
                     return $response;
                 } else {
                     $this->httpError(404, 'The requested page could not be found.');
                 }
                 return $this->response;
             }
             $locale = explode('-', $request->param('Language'));
             $locale[1] = strtoupper($locale[1]);
             //Make sure that the language is all lowercase
             if ($request->param('Language') == implode('-', $locale)) {
                 //Locale not found 404
                 if ($response = ErrorPage::response_for(404)) {
                     return $response;
                 } else {
                     $this->httpError(404, 'The requested page could not be found.');
                 }
                 return $this->response;
             }
             $locale = implode('_', $locale);
         } else {
             $locale = $request->param('Language');
         }
     } else {
         if (strpos($request->param('Language'), '_') !== false) {
             //Locale not found 404
             if ($response = ErrorPage::response_for(404)) {
                 return $response;
             } else {
                 $this->httpError(404, 'The requested page could not be found.');
             }
             return $this->response;
         } else {
             $locale = i18n::get_locale_from_lang($request->param('Language'));
         }
     }
     if (in_array($locale, Translatable::get_allowed_locales())) {
         //Set the current locale and remember it
         Cookie::set('language', $request->param('Language'));
         Translatable::set_current_locale($locale);
         i18n::set_locale($locale);
     } else {
         //Locale not found 404
         if ($response = ErrorPage::response_for(404)) {
             return $response;
         } else {
             $this->httpError(404, 'The requested page could not be found.');
         }
         return $this->response;
     }
     //Handle the home page for the language
     $urlSegment = $request->param('URLSegment');
     if (empty($urlSegment)) {
         $controller = new MultilingualRootURLController();
         $response = $controller->handleRequest($request, $model);
         $this->popCurrent();
         return $response;
     }
     //Normal page request so handle that
     $response = parent::handleRequest($request, $model);
     $this->popCurrent();
     return $response;
 }