views:

171

answers:

2

Hello guys, I'm trying to implement this code to have different files to load for german, spanish or english browser languages of choice. The case is that with my spanish IE I still get the english file.

<?php 
if (is_home()) {
  if (preg_match('/de-DE/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    include(TEMPLATEPATH . '/german-navbar.php' );
  }
  elseif (preg_match('/es-ES/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    include(TEMPLATEPATH . '/spanish-navbar.php' );
  }
  else {
    include(TEMPLATEPATH . '/english-navbar.php' );
  }
}

I used both HTTP_ACCEPT_LANGUAGE, and HTTP_USER_AGENT.

This is the test of the site, if anyone wanted to check it, with german or spanish as a language of choice in the browser : http://paragraphe.org/janette/

I have my Firefox in english and is working nice, but I can't be sure the code is working for spanish and german cases.

I found the snippet in this SO thread, but I'm a bit lost at this point.

Thanks so much for any input,

EDIT: the code does work in Firefox (but not IE).

+2  A: 

try this out please:

Just provide $availableLanguages as an array('en', 'de', 'es')

function get_client_language($availableLanguages, $default='en'){

if (isset($_SERVER['HTTP_ACCEPT_language'])) {

 $langs=explode(',',$_SERVER['HTTP_ACCEPT_language']);

 //start going through each one
 foreach ($langs as $value){

  $choice=substr($value,0,2);
  if(in_array($choice, $availableLanguages)){
   return $choice;

  }

 }
} 
return $default;

}

Sarfraz
+3  A: 

first of all, you can use the User Agent Switcher extension for firefox to fake useragents and test it, though you'd have to fake the headers of the requests for HTTP_ACCEPT_LANGUAGE, for instance with the Modify Headers extension (which also lets you change the user agents as well as the header)

oh, and it doesn't work in german:

Warning: include(/home/paragrap/public_html/janette/wp-content/themes/Janette/german-home.php) [function.include]: failed to open stream: No such file or directory in /home/paragrap/public_html/janette/wp-content/themes/Janette/home.php on line 4

Warning: include() [function.include]: Failed opening '/home/paragrap/public_html/janette/wp-content/themes/Janette/german-home.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/paragrap/public_html/janette/wp-content/themes/Janette/home.php on line 4
Zenon
Wow, that worked excellent, I could check it and the code is working now. Thanks !
Peanuts
glad I could help :)
Zenon