class UserAgent (View source)

Provides user agent related utility functions.

Methods

static string
getBestMatchingLangcode(string $http_accept_language, array $langcodes, array $mappings = [])

Identifies user agent language from the Accept-language HTTP header.

Details

static string getBestMatchingLangcode(string $http_accept_language, array $langcodes, array $mappings = [])

Identifies user agent language from the Accept-language HTTP header.

The algorithm works as follows:

  • map user agent language codes to available language codes.
  • order all user agent language codes by qvalue from high to low.
  • add generic user agent language codes if they aren't already specified but with a slightly lower qvalue.
  • find the most specific available language code with the highest qvalue.
  • if 2 or more languages are having the same qvalue, respect the order of them inside the $languages array.

We perform user agent accept-language parsing only if page cache is disabled, otherwise we would cache a user-specific preference.

Parameters

string $http_accept_language

The value of the "Accept-Language" HTTP header.

array $langcodes

An array of available language codes to pick from.

array $mappings

(optional) Custom mappings to support user agents that are sending non standard language codes. No mapping is assumed by default.

Return Value

string

The selected language code or FALSE if no valid language can be identified.