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

Breadcrumb

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

function Request::getLanguageComponents

Returns an array with the language components of the locale.

For example:

  • If the locale is "fr_Latn_FR", this method will return "fr", "Latn", "FR"
  • If the locale is "fr_FR", this method will return "fr", null, "FR"
  • If the locale is "zh_Hans", this method will return "zh", "Hans", null

Return value

array{string, string|null, string|null}

See also

https://wikipedia.org/wiki/IETF_language_tag

https://datatracker.ietf.org/doc/html/rfc5646

2 calls to Request::getLanguageComponents()
Request::formatLocale in vendor/symfony/http-foundation/Request.php
Strips the locale to only keep the canonicalized language value.
Request::getLanguageCombinations in vendor/symfony/http-foundation/Request.php
Returns an array of all possible combinations of the language components.

File

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

Class

Request
Request represents an HTTP request.

Namespace

Symfony\Component\HttpFoundation

Code

private static function getLanguageComponents(string $locale) : array {
    $locale = str_replace('_', '-', strtolower($locale));
    $pattern = '/^([a-zA-Z]{2,3}|i-[a-zA-Z]{5,})(?:-([a-zA-Z]{4}))?(?:-([a-zA-Z]{2}))?(?:-(.+))?$/';
    if (!preg_match($pattern, $locale, $matches)) {
        return [
            $locale,
            null,
            null,
        ];
    }
    if (str_starts_with($matches[1], 'i-')) {
        // Language not listed in ISO 639 that are not variants
        // of any listed language, which can be registered with the
        // i-prefix, such as i-cherokee
        $matches[1] = substr($matches[1], 2);
    }
    return [
        $matches[1],
        isset($matches[2]) ? ucfirst(strtolower($matches[2])) : null,
        isset($matches[3]) ? strtoupper($matches[3]) : null,
    ];
}

API Navigation

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