Tarayıcı Dili PHP yönlendirme çalışmıyor!

1 Cevap php

Ben URL bu gibi yapar php dizileri kullanarak dilleri değiştirmek:

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;
?>

Herhangi bir öneriniz?

1 Cevap

$_SERVER['HTTP_ACCEPT_LANGUAGE'] sadece 2 harfli ISO kodu daha karmaşıktır. Çıkış this question for a good answer on how to parse HTTP_ACCEPT_LANGUAGE PHP.