Fluent::set_persist_locale PHP Method

set_persist_locale() public static method

Not to be confused with the temporary locale assigned with {@see \Fluent::with_locale} .
public static set_persist_locale ( string $locale, string $key = null )
$locale string Locale to assign
$key string ID to set the locale against. Will automatically detect if omitted. Either Fluent:::config()->persist_id or Fluent::::config()->persist_id_cms.
    public static function set_persist_locale($locale, $key = null)
    {
        if (empty($key)) {
            $key = self::is_frontend() ? self::config()->persist_id : self::config()->persist_id_cms;
        }
        // Skip persist if key is unset
        if (empty($key)) {
            return;
        }
        // Save locale
        if ($locale) {
            Session::set($key, $locale);
        } else {
            Session::clear($key);
        }
        // Prevent unnecessarily excessive cookie assigment
        if (!headers_sent() && (!isset(self::$last_set_locale[$key]) || self::$last_set_locale[$key] !== $locale)) {
            self::$last_set_locale[$key] = $locale;
            Cookie::set($key, $locale, 90, null, null, false, false);
        }
    }

Usage Example

 public function handleRequest(SS_HTTPRequest $request, DataModel $model = null)
 {
     self::$is_at_root = true;
     $this->setDataModel($model);
     $this->pushCurrent();
     $this->init();
     $this->setRequest($request);
     // Check for existing routing parameters, redirecting to another locale automatically if necessary
     $locale = Fluent::get_request_locale();
     if (empty($locale)) {
         // Determine if this user should be redirected
         $locale = $this->getRedirectLocale();
         $this->extend('updateRedirectLocale', $locale);
         // Check if the user should be redirected
         $domainDefault = Fluent::default_locale(true);
         if (Fluent::is_locale($locale) && $locale !== $domainDefault) {
             // Check new traffic with detected locale
             return $this->redirect(Fluent::locale_baseurl($locale));
         }
         // Reset parameters to act in the default locale
         $locale = $domainDefault;
         Fluent::set_persist_locale($locale);
         $params = $request->routeParams();
         $params[Fluent::config()->query_param] = $locale;
         $request->setRouteParams($params);
     }
     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;
     }
     $localeURL = Fluent::alias($locale);
     $request->setUrl(self::fluent_homepage_link($localeURL));
     $request->match($localeURL . '/$URLSegment//$Action', true);
     $controller = new ModelAsController();
     $result = $controller->handleRequest($request, $model);
     $this->popCurrent();
     return $result;
 }
All Usage Examples Of Fluent::set_persist_locale