English 中文(简体)
Detecting the browser language of choice with PHP
原标题:

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).

最佳回答

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
问题回答

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;

}





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签