Plugin Directory

Changeset 1432257


Ignore:
Timestamp:
06/07/2016 10:15:14 AM (10 years ago)
Author:
bjoerne
Message:

Version 1.0.2: Consider all languages of HTTP_ACCEPT_LANGUAGE based on their q factor; Support country based locales like en-US; Try to match languages prefixes of country based locales as a fallback

Location:
language-redirect/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • language-redirect/trunk/language-redirect.php

    r1062413 r1432257  
    55Description: Redirects from the root site of a multisite project to a language specific network site.
    66Author: Björn Weinbrenner
    7 Version: 1.0.1
     7Version: 1.0.2
    88Author URI: http://www.bjoerne.com
    99*/
     
    3232    }
    3333    if ( array_key_exists( 'HTTP_ACCEPT_LANGUAGE', $_SERVER ) ) {
    34         $language = explode( ',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
    35         $language = strtolower( substr( chop( $language[0] ), 0, 2 ) );
    36         $redirect_location = language_redirect_get_redirect_location( $language );
     34        // Thanks to http://www.thefutureoftheweb.com/blog/use-accept-language-header for the following lines of code
     35        preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $header_matchings);
     36        if (count($header_matchings[1])) {
     37            // create a list like "en" => 0.8
     38            $language_to_priority_map = array_combine( $header_matchings[1], $header_matchings[4] );
     39            // set default to 1 for any without q factor
     40            foreach ($language_to_priority_map as $lang => $val) {
     41                if ( $val === '' ) $language_to_priority_map[$lang] = 1;
     42            }
     43            // sort list based on value
     44            arsort( $language_to_priority_map, SORT_NUMERIC );
     45        }
     46        $redirect_location = language_redirect_get_redirect_location( array_keys( $language_to_priority_map ) );
    3747    } else {
    3848        $redirect_location = language_redirect_get_default_redirect_location();
     
    5262    $request_protocol = array_key_exists( 'HTTPS', $_SERVER ) && $_SERVER['HTTPS'] ? 'https' : 'http';
    5363    $request_port     = $_SERVER['SERVER_PORT'] == '80' ? '' : ':'.$_SERVER['SERVER_PORT'];
    54     $request_url      = $request_protocol.'://'.$_SERVER['HTTP_HOST'].$request_port.$_SERVER['PHP_SELF'];
     64    $request_url      = $request_protocol . '://' . $_SERVER['HTTP_HOST'] . $request_port . $_SERVER['PHP_SELF'];
    5565    if ( strpos( $request_url, site_url() ) !== 0 ) {
    5666        return false;
     
    6373}
    6474
    65 function language_redirect_get_redirect_location( $language ) {
    66     $redirect_location = language_redirect_get_redirect_location_from_mapping( $language );
    67     if ( null != $redirect_location ) {
    68         return $redirect_location;
     75function language_redirect_get_redirect_location( $languages ) {
     76    foreach ( $languages as $language ) {
     77        $redirect_location = language_redirect_get_redirect_location_from_mapping( $language );
     78        if ( null != $redirect_location ) {
     79            return $redirect_location;
     80        }
     81    }
     82    foreach ( $languages as $language ) {
     83        $language_prefix = substr( $language, 0, 2 );
     84        $redirect_location = language_redirect_get_redirect_location_from_mapping( $language_prefix );
     85        if ( null != $redirect_location ) {
     86            return $redirect_location;
     87        }
    6988    }
    7089    return language_redirect_get_default_redirect_location();
     
    82101        }
    83102        $mapping_language = substr( $line, 0, $pos_of_equals );
    84         if ( $mapping_language == $language ) {
     103        if ( 0 === strcasecmp( $mapping_language, $language ) ) {
    85104            return substr( $line, $pos_of_equals + 1 );
    86105        }
  • language-redirect/trunk/readme.txt

    r1298292 r1432257  
    44Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XS98Y5ASSH5S4
    55Requires at least: 3.4
    6 Tested up to: 4.3.1
     6Tested up to: 4.3.4
    77Stable tag: trunk
    88License: GPLv3
     
    4040= 1.0.1 =
    4141* Handle missing HTTP_ACCEPT_LANGUAGE header, see [https://wordpress.org/support/topic/undefined-index-http_accept_language](https://wordpress.org/support/topic/undefined-index-http_accept_language)
    42 * Cleanup code to follow WP code conventions
     42* Cleanup code concerning WP code conventions
     43
     44= 1.0.2 =
     45* Consider all languages of HTTP_ACCEPT_LANGUAGE based on their q factor
     46* Support country based locales like en-US
     47* Try to match languages prefixes of country based locales as a fallback, e.g. given 'en-US' in header and 'en' in configuration
Note: See TracChangeset for help on using the changeset viewer.