Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Request.php

function Request::getPreferredLanguage

Returns the preferred language.

Parameters

string[] $locales An array of ordered available locales:

File

vendor/symfony/http-foundation/Request.php, line 1543

Class

Request
Request represents an HTTP request.

Namespace

Symfony\Component\HttpFoundation

Code

public function getPreferredLanguage(?array $locales = null) : ?string {
    $preferredLanguages = $this->getLanguages();
    if (!$locales) {
        return $preferredLanguages[0] ?? null;
    }
    $locales = array_map($this->formatLocale(...), $locales);
    if (!$preferredLanguages) {
        return $locales[0];
    }
    if ($matches = array_intersect($preferredLanguages, $locales)) {
        return current($matches);
    }
    $combinations = array_merge(...array_map($this->getLanguageCombinations(...), $preferredLanguages));
    foreach ($combinations as $combination) {
        foreach ($locales as $locale) {
            if (str_starts_with($locale, $combination)) {
                return $locale;
            }
        }
    }
    return $locales[0];
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal