How to trigger a site language change using php?

I am building a site using only PHP as a back-end (no frame), which should be available in three languages. The structure of the site (tables / divs) is perhaps less common. It tends to simulate Ajax behavior. I include the file (selector.php) in the body that takes care of it through _GET () passed from the navigation links. This means that the site actually never leaves index.php. Languages ​​are links (flags).

Now I managed to successfully implement 3 languages ​​(arrays / session / cookie) and it really works. However, I need to make a language choice to stay / refresh the same page from where it was called. It becomes difficult here, since I use only one page, each include / subsequent includes (30+) is called from index.php, so using header ('Location: ...') seems not an option. I know how to get my current address, including the GET parameter, so that I can use it. But how? Reading it at the beginning of the code, repeated reading in the language module corresponds to any given point. There is a session, so I could also use session variables, but instead of 35+ variables for the content (so the url changes) and match all the time, it seems not the right method. Where, or rather, how to catch a change in language and how to update / redirect to the "same page" for the changes to take effect? This may be a project stream.

Note 1: Using mod_rewrite , I redirected my language request, so now instead of mysite.com/index.php?lang=en it has the form mysite.com/en/index.php , so I can use more friendly links to call the contents of the body . <sh> Note 2: I missed the last week when I read / searched here / wherever Google leads, trying countless different approaches, none of them worked as expected (maybe a concept stream ?!).
Note 3: since this is for a client who explicitly asked not to use Java at all, do not offer Ajax / jQuery / Java solutions (with them I would know how to do this).
Note 4: The whole code is quite large to insert here, so if you need to see different parts, name them and I will edit my question.
Note 5: I still save myself as a newbie, so please bear with me.

Thanks in advance.

Thus, the language selector is as follows:

  session_start(); header('Cache-control: private'); // IE 6 FIX if(isSet($_GET['lang'])) { $lang = $_GET['lang']; $_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 = 'ro'; } switch ($lang) { case 'en': $lang_file = 'lang.en.php'; break; case 'it': $lang_file = 'lang.it.php'; break; case 'ro': $lang_file = 'lang.ro.php'; break; default: $lang_file = 'lang.ro.php'; break; } include_once 'languages/'.$lang_file; 

Then the selector ...

  function isValidPage($cont) { $validpages = array("history","mission","etc"); if(in_array($cont,$validpages) && file_exists(ltrim($cont,'/') . '.php')) { return TRUE; } else { return FALSE; } } if(isset($_GET['cont']) && isValidPage($_GET['cont'])) { @include($_GET['cont'] . '.php'); } else { @include('sub_index.php'); } 

I run my index.php with:

  <?php include_once 'lang_sel.php'; ?> <!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>mysite</title> <link rel="stylesheet" href="style/css.css" type="text/css" /> </head> <body> <div class="parent"> <div class="left"><?php include("main/left_main.php"); ?></div> <div class="right"> <div class="topmain"><?php include("main/top_main.php"); ?></div> <div class="dummyx"></div> <!-- dummy div for adjusting starting position --> <div class="rightall"> <?php include("main/top_nav.php"); ?> <?php /*include("main/top_rotator.php"); */?> <table width="780" border="0" cellpadding="10px" cellspacing="0" bgcolor="transparent" id="middle"><?php include ('selector.php'); ?></table> <!-- container for the main content --> </div> <div class="footer"> <?php include("main/bottom.php"); ?> <?php include("main/footer.php"); ?> </div> </div> <div class="clr"></div> <!-- clearing div attributes --> </div> 

Then the left side contains the flags:

 <tr> <td width="30">&nbsp; </td> <td width="63" align="center" valign="middle"><span class="lang"><a href="<?=WEBROOT?>ro/index.php" title="Romana"><img src="images/flags_ro.jpg" alt="ro" name="Romana" width="35" height="40" /></a></span></td> <td width="63" align="center" valign="middle"><span class="lang"><a href="<?=WEBROOT?>en/index.php" title="English"><img src="images/flags_en.jpg" alt="en" name="English" width="35" height="40" /></a></span></td> <td width="63" align="center" valign="middle"><span class="lang"><a href="<?=WEBROOT?>it/index.php" title="Italiano"><img src="images/flags_it.jpg" alt="it" name="Italiano" width="35" height="40" /></a></span></td> <td width="30">&nbsp; </td> </tr> 

Also a menu, which is a group of lists, for example:

  <ul class="menu"> <li><a href="index.php?cont=history" style="background:none"><?php echo $lang['MENU_HISTORY']; ?></a><!--<div><span>if you want to add 'sub-content' put it here</span></div>!--></li> <li><a href="index.php?cont=mission" style="background:none"><?php echo $lang['MENU_MISSION']; ?></a></li> <li><a href="index.php?cont=p_systems" style="background:none"><?php echo $lang['MENU_P_SYSTEM']; ?></a></li> <li><a href="index.php?cont=s_dist" style="background:none"><?php echo $lang['MENU_S_DIST']; ?></a></li> </ul> 

The content is simple, text and images. So the main code is above ...

+8
php
source share
2 answers

After 1 trillion trial / error, I finally did it! Maybe this is not the most elegant solution (i.e. Spartan), but it works. The trick was to read the current URL , blow it up to get the current page with the parameters, and then put it in the href flags.

So, at the beginning of index.php I set:

 <?php $url = $_SERVER['REQUEST_URI']; $pieces = explode("/", $url); ?> 

Then my flag links became:

 <a href="<?=WEBROOT . 'en/' . $pieces[3]?>" title="English"> 

where WEBROOT is my base url and folder, and $pieces[3] always my current page (ie index.php?cont=history ). That way, I always land on the current page, and the language changes accordingly.

Note: In my case. Part 3 contains the current page. If someone else uses this technique, be sure to check it out. Also, I only use one parameter all the time, so keep that in mind (it doesn't really matter).

If someone knows a better solution, I am open. Thanks for everyone.

0
source share
0
source share

All Articles