3

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
  • 1
    What does die($_SERVER['HTTP_ACCEPT_LANGUAGE']); give you? Commented Nov 19, 2009 at 13:26
  • The page just loads as usual - but with the english default, as if the other conditionals had no meaning Commented Nov 19, 2009 at 13:33

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, that worked excellent, I could check it and the code is working now. Thanks !
2

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.