views:

29

answers:

1

I change languages using php arrays which makes the URL look like this:

http://alexchen.zxq.net/index.php?lang=es

I added $lang=$_SERVER['HTTP_ACCEPT_LANGUAGE']; in my 'controller' file in default sections, and I choose another language in the preferred languages option (Mozilla Firefox), but it didn't work.

common.php:

<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang'])) {
 $lang = $_GET['lang'];

 // register the session and set the cookie
 $_SESSION['lang'] = $lang;

 setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang'])) {
 $lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang'])) {
 $lang = $_COOKIE['lang'];
}
else {
 //$lang = 'en'; <-this was previous code
        //I tried this:
        $lang=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
}

// use appropiate lang.xx.php file according to the value of the $lang
switch ($lang) {
case 'en':
 $lang_file = 'lang.en.php';
 break;

case 'es':
 $lang_file = 'lang.es.php';
 break;

case 'tw':
 $lang_file = 'lang.tw.php';
 break;

case 'cn':
 $lang_file = 'lang.cn.php';
 break;

default:
 //$lang_file = 'lang.en.php'; <-this was before
        //I also tried this:
        $lang=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
}

//translation helper function
function l($translation) {
 global $lang;
 return $lang[$translation];
}

 include_once 'languages/'.$lang_file;
?>

Any suggestions?

+1  A: 

$_SERVER['HTTP_ACCEPT_LANGUAGE'] is more complex than just a 2-letter ISO code. Check out this question for a good answer on how to parse HTTP_ACCEPT_LANGUAGE in PHP.

Pekka