function language_from_url

Identify language via URL prefix or domain.

Parameters

$languages: An array of valid language objects.

\Symfony\Component\HttpFoundation\Request|null $request: (optional) The HttpRequest object representing the current request. Defaults to NULL.

Return value

A valid language code on success, FALSE otherwise.

2 string references to 'language_from_url'
language_language_negotiation_info in drupal/core/modules/language/language.module
Implements hook_language_negotiation_info().
locale_update_8007 in drupal/core/modules/locale/locale.install
Convert language_negotiation_* variables to use the new callbacks.

File

drupal/core/modules/language/language.negotiation.inc, line 276
Language negotiation functions.

Code

function language_from_url($languages, Request $request = NULL) {
  $language_url = FALSE;
  if (!language_negotiation_method_enabled(LANGUAGE_NEGOTIATION_URL) || !$request) {
    return $language_url;
  }
  switch (config('language.negotiation')
    ->get('url.source')) {
    case LANGUAGE_NEGOTIATION_URL_PREFIX:
      $request_path = urldecode(trim($request
        ->getPathInfo(), '/'));
      list($language, $path) = language_url_split_prefix($request_path, $languages);
      if ($language !== FALSE) {
        $language_url = $language->langcode;
      }
      break;
    case LANGUAGE_NEGOTIATION_URL_DOMAIN:

      // Get only the host, not the port.
      $http_host = $_SERVER['HTTP_HOST'];
      if (strpos($http_host, ':') !== FALSE) {
        $http_host_tmp = explode(':', $http_host);
        $http_host = current($http_host_tmp);
      }
      $domains = language_negotiation_url_domains();
      foreach ($languages as $language) {

        // Skip the check if the language doesn't have a domain.
        if (!empty($domains[$language->langcode])) {

          // Ensure that there is exactly one protocol in the URL when checking
          // the hostname.
          $host = 'http://' . str_replace(array(
            'http://',
            'https://',
          ), '', $domains[$language->langcode]);
          $host = parse_url($host, PHP_URL_HOST);
          if ($http_host == $host) {
            $language_url = $language->langcode;
            break;
          }
        }
      }
      break;
  }
  return $language_url;
}