Changeset 1432257
- Timestamp:
- 06/07/2016 10:15:14 AM (10 years ago)
- Location:
- language-redirect/trunk
- Files:
-
- 2 edited
-
language-redirect.php (modified) (5 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
language-redirect/trunk/language-redirect.php
r1062413 r1432257 5 5 Description: Redirects from the root site of a multisite project to a language specific network site. 6 6 Author: Björn Weinbrenner 7 Version: 1.0. 17 Version: 1.0.2 8 8 Author URI: http://www.bjoerne.com 9 9 */ … … 32 32 } 33 33 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 ) ); 37 47 } else { 38 48 $redirect_location = language_redirect_get_default_redirect_location(); … … 52 62 $request_protocol = array_key_exists( 'HTTPS', $_SERVER ) && $_SERVER['HTTPS'] ? 'https' : 'http'; 53 63 $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']; 55 65 if ( strpos( $request_url, site_url() ) !== 0 ) { 56 66 return false; … … 63 73 } 64 74 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; 75 function 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 } 69 88 } 70 89 return language_redirect_get_default_redirect_location(); … … 82 101 } 83 102 $mapping_language = substr( $line, 0, $pos_of_equals ); 84 if ( $mapping_language == $language) {103 if ( 0 === strcasecmp( $mapping_language, $language ) ) { 85 104 return substr( $line, $pos_of_equals + 1 ); 86 105 } -
language-redirect/trunk/readme.txt
r1298292 r1432257 4 4 Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XS98Y5ASSH5S4 5 5 Requires at least: 3.4 6 Tested up to: 4.3. 16 Tested up to: 4.3.4 7 7 Stable tag: trunk 8 8 License: GPLv3 … … 40 40 = 1.0.1 = 41 41 * 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.