Chris@0: $standard_langcode) { Chris@0: if ($langcode == $ua_langcode) { Chris@0: $match[1] = $standard_langcode; Chris@0: } Chris@0: } Chris@0: } Chris@0: // We can safely use strtolower() here, tags are ASCII. Chris@0: // RFC2616 mandates that the decimal part is no more than three digits, Chris@0: // so we multiply the qvalue by 1000 to avoid floating point Chris@0: // comparisons. Chris@0: $langcode = strtolower($match[1]); Chris@0: $qvalue = isset($match[2]) ? (float) $match[2] : 1; Chris@0: // Take the highest qvalue for this langcode. Although the request Chris@0: // supposedly contains unique langcodes, our mapping possibly resolves Chris@0: // to the same langcode for different qvalues. Keep the highest. Chris@0: $ua_langcodes[$langcode] = max( Chris@0: (int) ($qvalue * 1000), Chris@0: (isset($ua_langcodes[$langcode]) ? $ua_langcodes[$langcode] : 0) Chris@0: ); Chris@0: } Chris@0: } Chris@0: Chris@0: // We should take pristine values from the HTTP headers, but Internet Chris@0: // Explorer from version 7 sends only specific language tags (eg. fr-CA) Chris@0: // without the corresponding generic tag (fr) unless explicitly configured. Chris@0: // In that case, we assume that the lowest value of the specific tags is the Chris@0: // value of the generic language to be as close to the HTTP 1.1 spec as Chris@0: // possible. Chris@0: // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4 and Chris@0: // http://blogs.msdn.com/b/ie/archive/2006/10/17/accept-language-header-for-internet-explorer-7.aspx Chris@0: asort($ua_langcodes); Chris@0: foreach ($ua_langcodes as $langcode => $qvalue) { Chris@0: // For Chinese languages the generic tag is either zh-hans or zh-hant, so Chris@0: // we need to handle this separately, we can not split $langcode on the Chris@0: // first occurrence of '-' otherwise we get a non-existing language zh. Chris@0: // All other languages use a langcode without a '-', so we can safely Chris@0: // split on the first occurrence of it. Chris@0: if (strlen($langcode) > 7 && (substr($langcode, 0, 7) == 'zh-hant' || substr($langcode, 0, 7) == 'zh-hans')) { Chris@0: $generic_tag = substr($langcode, 0, 7); Chris@0: } Chris@0: else { Chris@0: $generic_tag = strtok($langcode, '-'); Chris@0: } Chris@0: if (!empty($generic_tag) && !isset($ua_langcodes[$generic_tag])) { Chris@0: // Add the generic langcode, but make sure it has a lower qvalue as the Chris@0: // more specific one, so the more specific one gets selected if it's Chris@0: // defined by both the user agent and us. Chris@0: $ua_langcodes[$generic_tag] = $qvalue - 0.1; Chris@0: } Chris@0: } Chris@0: Chris@0: // Find the added language with the greatest qvalue, following the rules Chris@0: // of RFC 2616 (section 14.4). If several languages have the same qvalue, Chris@0: // prefer the one with the greatest weight. Chris@0: $best_match_langcode = FALSE; Chris@0: $max_qvalue = 0; Chris@0: foreach ($langcodes as $langcode_case_sensitive) { Chris@0: // Language tags are case insensitive (RFC2616, sec 3.10). Chris@0: $langcode = strtolower($langcode_case_sensitive); Chris@0: Chris@0: // If nothing matches below, the default qvalue is the one of the wildcard Chris@0: // language, if set, or is 0 (which will never match). Chris@0: $qvalue = isset($ua_langcodes['*']) ? $ua_langcodes['*'] : 0; Chris@0: Chris@0: // Find the longest possible prefix of the user agent supplied language Chris@0: // ('the language-range') that matches this site language ('the language Chris@0: // tag'). Chris@0: $prefix = $langcode; Chris@0: do { Chris@0: if (isset($ua_langcodes[$prefix])) { Chris@0: $qvalue = $ua_langcodes[$prefix]; Chris@0: break; Chris@0: } Chris@0: } while ($prefix = substr($prefix, 0, strrpos($prefix, '-'))); Chris@0: Chris@0: // Find the best match. Chris@0: if ($qvalue > $max_qvalue) { Chris@0: $best_match_langcode = $langcode_case_sensitive; Chris@0: $max_qvalue = $qvalue; Chris@0: } Chris@0: } Chris@0: Chris@0: return $best_match_langcode; Chris@0: } Chris@0: Chris@0: }