I was wondering how I could make users of a website change the language of the site. If for example I would have 2 language files: lang_eng.php lang_spa.php with all the language variables in there. I was thinking about having a drop down box on top of the site where users could change the language of the site. Im search in google and find this: make the dropdown form, action="changeLang.php" next, you will need to either use a cookie/session to store their language choice, or put it in the URL /* changeLang.php */ $_SESSION['Language'] = $_POST['Language']; // setcookie("Language", $_POST['Language'], time() + 3600); // URL method will require you to add the QUERY STRING to all pages. might get tough if($Language == 'English') { include 'English.php'; PHP: I have the languages files and the code for call the language, but how i can add this function to the dropdown menu and create the changeLang.php, please somebody can help me? thanks
OK on the form where people changes there language you add the action attribute. So it would be something like this: <form action="changeLang.php" method="post"> //Add your drop down box and submit button here </form> Then on changeLang.php you create a session or a cookie whicever method you choose, and store their language choice on there. Then on all the pages where you change the language you check what language is stored in the session/cookie and include the file based on this choice.
HI, i created the form, but how can i add the cookie in changeLang.php, the code is: $_SESSION['Language'] = $_POST['Language']; but how can i add for works. Sorry for my english im mexican.
OK firstly don't forget that for Sessions to work, you must have: session_start(); as the first line on ALL of the pages you want to use your sessions on or transfer sessions to. Secondly on changeLang.php you are correct you would have somethinglike: $_SESSION['Language'] = $_POST['Language']; Code (markup): Then on all pages you have a language option you would do: if($_SESSION['Language']){ //Then a language has previously been selected include the language here } else{ //None has been selected, use the default language. } Code (markup):
ok, the page where i go to put the language is the header, because all page take the language from header, now, im add this code to the header : include_once($web_root."includes/sources/init.php"); if($_SESSION['English']){ include_once($web_root."includes/english.php"); } if($_SESSION['Espanol']){ include_once($web_root."includes/spanish.php"); } else{ include_once($web_root."includes/spanish.php"); } But only what still dont work is the changeLang.php because dont load or created any cookie or session
Hi, Please paste your changeLang.php as it is now.. And also please paste the form where people can choose the language Please paste both files exactly !! Thanks
The changeLang: <?php $value = "Language"; // send a cookie that expires in 24 hours setcookie("Language",$_POST['Language'], time()+3600*24*30); ?> PHP: The form: <form action="changeLang.php" method="post"> <label> <select name="language" size="1" id="language"> <option value="es">Español</option> <option value="en">English</option> </select> </label> <label> <input type="submit" name="submit" id="submit" value="Cambiar Idioma"> </label> </form> PHP: The header: <?php session_start(); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); header("Cache-Control: no-cache, must-revalidate" ); header("Pragma: no-cache" ); if(file_exists('../includes/constants.php')){ include_once "../includes/constants.php"; include_once "../includes/rating_functions.php"; } else { include_once "includes/constants.php"; include_once "includes/rating_functions.php"; } ?> <script src="<?=$web_path;?>/topbar/top1.php" type="text/javascript"></script> <script src="<?=$web_path;?>/peels/peel/peel.js" type="text/javascript"></script> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' /> <link href="<?=$web_path;?>css/music.css" type="text/css" rel="stylesheet" /> <title><? if (isset($_GET[search])) { echo "$search - "; } ?><?=$settings['site_title'];?></title> <meta name="keywords" content="<?=$settings['site_keywords'];?>" /> <meta name="description" content="<?=$settings['site_desc'];?>" /> <link rel="stylesheet" href="<?=$web_path;?>css/pop.css" media="screen,projection" type="text/css" /> <link rel="stylesheet" href="<?=$web_path;?>css/style.css" media="screen,projection" type="text/css" /> <link rel="stylesheet" href="<?=$web_path;?>css/rating_style.css" media="screen,projection" type="text/css" /> <? include $web_root."css/style.php"; include $web_root."css/rating_style.php"; include $web_root."includes/functions.php"; include_once($web_root."includes/sources/init.php"); if($_SESSION['English']){ include_once($web_root."includes/english.php"); } if($_SESSION['Espanol']){ include_once($web_root."includes/spanish.php"); } else{ include_once($web_root."includes/spanish.php"); } ?> <script language="JavaScript" type="text/javascript" src="<?=$web_path;?>js/ajax_search.js"></script> <script type="text/javascript" src="<?=$web_path;?>js/prototype.js"></script> <script type="text/javascript" src="<?=$web_path;?>js/pop.js"></script> <script type="text/javascript" src="<?=$web_path;?>js/tooltip.js"></script> <script type="text/javascript" src="<?=$web_path;?>js/display.js"></script> <script type="text/javascript" src="<?=$web_path;?>js/email.js"></script> <script type="text/javascript" src="<?=$web_path;?>js/load.js"></script> </head> <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("UA-5762938-2"); pageTracker._trackPageview(); } catch(err) {}</script> <body> <!-- Main Container --> <div class="clear_main"> <div class="topband"> <div class="top_left"></div> <div class="top_menu"> <? if($session->logged_in){?> <span><?=$lang['Welcome'];?> <? echo $session->username; ?></span> <span>|</span> <a href="<?=$web_path;?>userinfo.php?user=<? echo $session->username; ?>"><?=$lang['AccountHome'];?></a> <span>|</span> <? if($session->isAdmin()){?> <a href="<?=$web_path;?>mu" class="white"></a> <a href="<?=$web_path;?>admin/admin.php"><?=$lang['AdminPanel'];?></a> <span>|</span> <? } ?> <a href="<?=$web_path;?>myplaylist"><?=$lang['MyPlaylist'];?></a> <span>|</span> <a href="<?=$web_path;?>mu" class="white"><?=$lang['DescargaMV'];?></a> <span>|</span> <a href="<?=$web_path;?>/old/tutoriales" target="_self">Tutoriales</a></li> <a href="<?=$web_path;?>faq.php" class="white"> | <?=$lang['FAQ'];?> </a> <span>|</span> <a href="<?=$web_path;?>process.php" class="white"><?=$lang['Logout'];?></a> <? } else { ?> <?=$lang['NotaMember'];?> <a href="<?=$web_path;?>register.php"><?=$lang['Signup'];?></a> <span>|</span> <a href="<?=$web_path;?>main.php"><?=$lang['MemberLogin'];?></a> <span>|</span> <a href="<?=$web_path;?>/old/tutoriales" target="_self">Tutoriales</a></li> <span>|</span> </a> <a class="white" href="<?=$web_path;?>faq.php"><?=$lang['FAQ'];?></a> <? } ?> </div> <div class="top_right"></div> </div> PHP:
should be: include_once($web_root."includes/sources/init.php"); if($_SESSION['Language']=="English"){ include_once($web_root."includes/english.php"); } if($_SESSION['Language']=="Espanol"){ include_once($web_root."includes/spanish.php"); } else{ include_once($web_root."includes/spanish.php"); } PHP:
OK i'm going to do the example based on Sessions and not Cookies. change changeLang.php to this: <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php if($_POST){ //Then somebody has submitted the form $lang = $_POST['language']; //Test Text echo "<h1>You want to change your language to $lang</h1>"; //Create Session Variable $_SESSION['lang'] = $lang; //Test Session Variable was created echo "<h2>We have just created a Session with the value".$_SESSION['lang']"</h2>"; } ?> </body> </html> PHP: Then change the language part in your header from: if($_SESSION['English']){ include_once($web_root."includes/english.php"); } if($_SESSION['Espanol']){ include_once($web_root."includes/spanish.php"); } else{ include_once($web_root."includes/spanish.php"); } PHP: to this: if($_SESSION['lang']){ //A session has been created with a selected language echo "<h1>A Language has previously been chosen with the value:".$_SESSION['lang']."</h1>"; if($_SESSION['lang'] == "en"){ echo "<h2>This user has previously selected English as their language choice</h2>"; include_once($web_root."includes/english.php"); } elseif($_SESSION['Espanol']){ echo "<h2>This user has previously selected English as their language choice</h2>"; include_once($web_root."includes/spanish.php"); } else{ echo "<h2>An unknown choice is stored in the lang</h2>"; } } else{ //A language has not yet been selected by the user, so use the default language include_once($web_root."includes/spanish.php"); } PHP: Note: It will output messages, I have done this on purpose so we know what's going on at each stage, we can delete these messages after it is working. Let me know if it is working, or if not what messages are being shown and when..
Only show me an error in changelang: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/mimp3/domains/mimp3.net/public_html/changeLang.php on line 21
Sorry changeLang.php: <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php if($_POST){ //Then somebody has submitted the form $lang = $_POST['language']; //Test Text echo "<h1>You want to change your language to $lang</h1>"; //Create Session Variable $_SESSION['lang'] = $lang; //Test Session Variable was created echo "<h2>We have just created a Session with the value".$_SESSION['lang']."</h2>"; } ?> </body> </html> PHP: Just missed a fullstop
<?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php if($_SERVER['REQUEST_METHOD']=="POST"){ //Then somebody has submitted the form $lang = $_POST['language']; //Test Text echo "<h1>You want to change your language to $lang</h1>"; //Create Session Variable $_SESSION['lang'] = $lang; //Test Session Variable was created echo "<h2>We have just created a Session with the value $lang</h2>"; } ?> </body> </html> PHP:
Works, thanks man, you are awsome, only i want delete all messages, and when the user select the language send to index.php And how much time the session is started,
For redirecting to index just do: <?php session_start(); if($_SERVER['REQUEST_METHOD']=="POST"){ //Then somebody has submitted the form $lang = $_POST['language']; //Create Session Variable $_SESSION['lang'] = $lang; header('location: index.php'); } ?> YOUR HTML PHP:
Thanks to both, works great, only i have a question, its possible created the session by a link and no a drop menu?
changelog.php <?php session_start(); if($_SERVER['REQUEST_METHOD']=="GET"){ //Then somebody has submitted the form $lang = $_GET['language']; //Create Session Variable $_SESSION['lang'] = $lang; header('location: index.php'); } ?> YOUR HTML PHP: And use the link English: http://www.yoursite.com/changelog.php?lang=en Espanol: http://www.yoursite.com/changelog.php?lang=es