Fluent::detect_browser_locale PHP Method

detect_browser_locale() public static method

Determines the locale best matching the given list of browser locales
public static detect_browser_locale ( mixed $domain = null ) : string
$domain mixed Domain to determine the locales for. If null, the global list be returned. If true, then the current domain will be used.
return string The matching locale, or null if none could be determined
    public static function detect_browser_locale($domain = null)
    {
        // Check for empty case
        if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
            return null;
        }
        // Given multiple canditates, narrow down the final result using the client's preferred languages
        $inputLocales = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
        if (empty($inputLocales)) {
            return null;
        }
        // Generate mapping of priority => list of locales at this priority
        // break up string into pieces (languages and q factors)
        preg_match_all('/(?<code>[a-z]{1,8}(-[a-z]{1,8})?)\\s*(;\\s*q\\s*=\\s*(?<priority>1|0\\.[0-9]+))?/i', $inputLocales, $parsedLocales);
        $prioritisedLocales = array();
        if (count($parsedLocales['code'])) {
            // create a list like "en" => 0.8
            $parsedLocales = array_combine($parsedLocales['code'], $parsedLocales['priority']);
            // Generate nested list of priorities => [locales]
            foreach ($parsedLocales as $locale => $priority) {
                $priority = empty($priority) ? 1.0 : floatval($priority);
                if (empty($prioritisedLocales[$priority])) {
                    $prioritisedLocales[$priority] = array();
                }
                $prioritisedLocales[$priority][] = $locale;
            }
            // sort list based on value
            krsort($prioritisedLocales, SORT_NUMERIC);
        }
        // Check each requested locale against loaded locales
        foreach ($prioritisedLocales as $priority => $parsedLocales) {
            foreach ($parsedLocales as $browserLocale) {
                foreach (self::locales($domain) as $locale) {
                    if (stripos(preg_replace('/_/', '-', $locale), $browserLocale) === 0) {
                        return $locale;
                    }
                }
            }
        }
        return null;
    }

Usage Example

コード例 #1
0
 /**
  * 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::detect_browser_locale