Fluent::get_persist_locale PHP Method

get_persist_locale() public static method

Gets the locale currently set within either the session or cookie.
public static get_persist_locale ( string $key = null ) : string | null
$key string ID to retrieve persistant locale from. Will automatically detect if omitted. Either Fluent::config()->persist_id or Fluent::config()->persist_id_cms.
return string | null The locale, if available
    public static function get_persist_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 null;
        }
        // check session then cookies
        if ($locale = Session::get($key)) {
            return $locale;
        }
        if ($locale = Cookie::get($key)) {
            return $locale;
        }
    }

Usage Example

 /**
  * For incoming traffic to the site root, determine if they should be redirected to any locale.
  *
  * @return string|null The locale to redirect to, or null
  */
 protected function getRedirectLocale()
 {
     // Redirection interfere with flushing, so don't redirect
     if (isset($_GET['flush'])) {
         return null;
     }
     // Don't redirect if the user has clicked a link on the locale menu
     if ($this->knownReferrer()) {
         return null;
     }
     // Redirect if this user has previously viewed a page in any locale
     if (Fluent::config()->remember_locale && ($locale = Fluent::get_persist_locale())) {
         return $locale;
     }
     // Detect locale from browser Accept-Language header
     if (Fluent::config()->detect_locale && ($locale = Fluent::detect_browser_locale())) {
         return $locale;
     }
 }
All Usage Examples Of Fluent::get_persist_locale