Exemplo n.º 1
0
 /**
  * @param  TheliaRequest $request
  * @return null|\Thelia\Model\Lang
  */
 protected function detectLang(TheliaRequest $request)
 {
     // first priority => lang parameter present in request (get or post)
     if ($request->query->has("lang")) {
         // The lang parameter may contains a lang code (fr, en, ru) for Thelia < 2.2,
         // or a locale (fr_FR, en_US, etc.) for Thelia > 2.2.beta1
         $requestedLangCodeOrLocale = $request->query->get("lang");
         if (strlen($requestedLangCodeOrLocale) > 2) {
             $lang = LangQuery::create()->findOneByLocale($requestedLangCodeOrLocale);
         } else {
             $lang = LangQuery::create()->findOneByCode($requestedLangCodeOrLocale);
         }
         if (is_null($lang)) {
             return Lang::getDefaultLanguage();
         }
         // if each lang has its own domain, we redirect the user to the proper one.
         if (ConfigQuery::isMultiDomainActivated()) {
             $domainUrl = $lang->getUrl();
             if (!empty($domainUrl)) {
                 // if lang domain is different from current domain, redirect to the proper one
                 if (rtrim($domainUrl, "/") != $request->getSchemeAndHttpHost()) {
                     // TODO : search if http status 302 is the good one.
                     return new RedirectResponse($domainUrl, 302);
                 } else {
                     //the user is currently on the proper domain, nothing to change
                     return null;
                 }
             }
             Tlog::getInstance()->warning("The domain URL for language " . $lang->getTitle() . " (id " . $lang->getId() . ") is not defined.");
             return Lang::getDefaultLanguage();
         } else {
             // one domain for all languages, the lang has to be set into session
             return $lang;
         }
     }
     // Next, check if lang is defined in the current session. If not we have to set one.
     if (null === $request->getSession()->getLang(false)) {
         if (ConfigQuery::isMultiDomainActivated()) {
             // find lang with domain
             return LangQuery::create()->filterByUrl($request->getSchemeAndHttpHost(), ModelCriteria::LIKE)->findOne();
         }
         // At this point, set the lang to the default one.
         return Lang::getDefaultLanguage();
     }
     return null;
 }