Defining a browser language using PHP

I am trying to implement this code in order to have different files for downloading into the German, Spanish or English languages ​​of the browser of my choice. The fact is that with my Spanish IE, I still get an 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 a site test, if someone wants to test it, with German or Spanish as the preferred language in the browser: http://paragraphe.org/janette/

I have my Firefox in English and it works well, but I can’t be sure that the code works in both Spanish and German cases.

I found a snippet in this SO thread , but at the moment I am a little lost.

Thanks so much for any input,

EDIT: The code works in Firefox (but not in IE).

+3
2

, User Agent Switcher firefox useragents , HTTP_ACCEPT_LANGUAGE, , Modify Headers ( , )

oh, :

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
+3

, :

$availableLanguages ​​ ('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;

}

+2

Source: https://habr.com/ru/post/1723342/


All Articles