1.20.x locale.inc | locale_language_from_url($languages) |
Identify language via URL prefix or domain.
Parameters
$languages: An array of valid language objects.
Return value
A valid language code on success, FALSE otherwise.:
Related topics
File
- includes/
locale.inc, line 263 - Administration functions for locale.module.
Code
function locale_language_from_url($languages) {
$language_url = FALSE;
if (!language_negotiation_get_any(LANGUAGE_NEGOTIATION_URL)) {
return $language_url;
}
switch (config_get('locale.settings', 'language_negotiation_url_part')) {
case LANGUAGE_NEGOTIATION_URL_PREFIX:
// $_GET['q'] might not be available at this time, because
// path initialization runs after the language bootstrap phase.
list($language, $_GET['q']) = language_url_split_prefix(isset($_GET['q']) ? $_GET['q'] : NULL, $languages);
if ($language !== FALSE) {
$language_url = $language->langcode;
}
break;
case LANGUAGE_NEGOTIATION_URL_DOMAIN:
$domains = locale_language_negotiation_url_domains();
// 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);
}
foreach ($languages as $language) {
// Skip check if the language doesn't have a domain.
if (!empty($domains[$language->langcode])) {
// Only compare the domains not the protocols or ports.
// Remove protocol and add http:// so parse_url works
$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;
}