Multilingual PHP script question
I have this PHP script. This is the only thing that really worked with 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*/
}
}
?>
The code first detects the language of the user's browser. This is normal.
It then stores the information in a cookie. This is normal.
Well, everything is fine in this piece of code. I really need to create an option for language changing visitors. I was thinking something like related flags images, so when someone clicks on the flag it changes the language.
Can someone please explain me with an example, or even a clean, complete solution? My PHP skills are poor.
Thanks in advance.
I'm not sure if I got your question right.
if you put a link for each language on your page make a link something like http://www.example.com/?lang=jp
then in the php code before the script you posted add
if (isset($_GET['lang'])) $lang = $_GET['lang'];
is this what you think?
a source to share
A more elegant solution might be to check user titles. Most browsers will allow users to set their preferred language in preferences. This, in turn, sends an HTTP request with the request. The title 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-separated list of recognized languages, ordered by preference (q = x part is preferred). This way, you can automatically detect which language the user has chosen to view on the web and display it if you have one.
a source to share