Fluent::get_request_locale PHP Method

get_request_locale() public static method

Gets the locale requested directly in the request, either via route, post, or query parameters
public static get_request_locale ( ) : string
return string The locale, if available
    public static function get_request_locale()
    {
        $locale = null;
        // Check controller and current request
        if (Controller::has_curr()) {
            $controller = Controller::curr();
            $request = $controller->getRequest();
            if (self::is_frontend()) {
                // If viewing the site on the front end, determine the locale from the viewing parameters
                $locale = $request->param(self::config()->query_param);
            } else {
                // If viewing the site from the CMS, determine the locale using the session or posted parameters
                $locale = $request->requestVar(self::config()->query_param);
            }
        }
        // Without controller check querystring the old fashioned way
        if (empty($locale) && isset($_REQUEST[self::config()->query_param])) {
            $locale = $_REQUEST[self::config()->query_param];
        }
        return $locale;
    }

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;
 }