views:

410

answers:

4

Hi folks. I have this PHP script. It's the only one that really worked to me:

<?php
/*Check_if_user_has_changed_language: */
if(isset($lang)){/*If_so:*/
    setcookie("ling",$lang,time()-60*60*24*365,"/",".sayip.info",0);/*Wipe_previous_cookie*/
    setcookie("ling",$lang,time()+60*60*24*365,"/",".sayip.info",0);/*Whatever_the_means_lang_has_been_stored,_store_latest_lang_in_new_cookie:*/
    //echo "<script language=\"JavaScript\">alert('Selected language=$lang')</script>";/*UnComment_to_check*/
}else{/*If_user_has_NOT_changed_language:*/
    if(isset($_COOKIE['ling'])){/*Check_if_user-language_cookie_is_set._If_so:*/
        $lang=$_COOKIE['ling'];
        setcookie("ling",$lang,time()-60*60*24*365,"/",".sayip.info",0);/*Wipe_previous_cookie*/
        setcookie("ling",$lang,time()+60*60*24*365,"/",".sayip.info",0);
        //echo "<script language=\"JavaScript\">alert('Cookie language=$lang')</script>";/*UnComment_to_check*/
    }else{/*If_user-language_neither_selected_nor_in_cookie,_choose_browser_language:*/
        $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);
        setcookie("ling",$lang,time()+60*60*24*365,"/",".sayip.info",0);
        //echo "<script language=\"JavaScript\">alert('Your browser language=$lang')</script>";/*UnComment_to_check*/
    }
}
?>

First the code detects the language of the user's browser. That's ok.

Then stores the info in a cookie. That's ok.

Well in this piece of code its everything ok. What I really need is to create an option for visitors change the language. I was thinking something like linked flag images so when someone click on the flag it changes the language.

Can someone explain to me through an example or even a clean, full solution? My skills in PHP are poor.

Thanks in advance.

A: 

I would put the selected language in the URL, e.g http://example.com/en/foo/bar. This makes the selected language transparent and easy to change.

Gumbo
A: 

i would put to if i knew how to lol...Since i got this script working after testing more or less 10 different scripts, im not like falling back, just need an example in how to put flags and when someone click on the flag it changes de value of the cookie...

Use the comment function below the anwser you want to comment to.
Gumbo
A: 

I'm not sure if I got your question right

if your gonna place a link for each language in your page, make the link something like http://www.example.com/?lang=jp

then in the php code before the script that you posted add

if (isset($_GET['lang'])) $lang = $_GET['lang'];

is this what you ment?

hayato
A: 

A more elegant solution may be to check the user's headers. Most browsers will allow users to set their preferred language in preferences. This in turn sends an HTTP header with the request. The header looks like this.

Accept-Language : en-us,en;q=0.8,ar-ly;q=0.5,id;q=0.3

The value is a comma-delimited list of accepted languages, ordered by preference ( the q=x part is the preference). This way, you can automatically detect what language the user has opted to see the web, and display it if you have it.

Chris Henry