By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,005 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,005 IT Pros & Developers. It's quick & easy.

Language redirect

P: 2
I have been struggling with this problem for over a week and am not much nearer a solution. I am trying to create a multi language site that only requires the content to be added as simple text or html files although php files can be included. I have a page that detects the language used on the users browser:
Expand|Select|Wrap|Line Numbers
  1. <?
  2. class object {};
  3. $config = new object;
  4. $config->dir     = "/home/path/public_html/language2/lang_sets/";
  5. if (! isset($lang)) {
  6.     $lang = "pt";
  7.     $lang = "en";
  8.     $lang = "lv";
  9. }
  10. else if ($lang == "pt-br" ) { $SESSION["lang"] = "pt"; }
  11. else if ($lang == "en" ) { $SESSION["lang"] = "en"; }
  12. else if ($lang == "lv" ) { $SESSION["lang"] = "lv"; }
  13. else { $SESSION["lang"] = "en"; }
  14. $config->global = $config->dir.$lang."/language.php";
  15. include ($config->global);
  16. ?>
  17. <html>
  18. <head>
  19. <title>webwhiz language2</title></head>
  20. <body>
  21. <?=$content4?>
  22. <?=$content1?>
  23. <?=$content2?>
  24. </body>
  25. </html>
The $content variables can contain php includes contained in the individual language_sets language.php files:

Expand|Select|Wrap|Line Numbers
  1. <?
  2. $content1 = include_once("../include/langselect.php");
  3. $content2 = include_once("../include/contactform.php");
  4. $content4 = include_once("../include/footer.php");
  5. ?>
In the code above, I want to allow the user to select a different language if required. I found a snippet on this site which will do this but have come unstuck. What I want is for the include/langselect.php file above to allow the user to redirect to the index file of their choice. In this instance for the english page: /language_sets/en/language.php. So far in the include/langselect.php file, I have the code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $language = $_GET["language"];
  4.  
  5. ?>
  6.  
  7. <select onchange="window.location='select.php?language='+this.value">
  8.  
  9. <?php
  10.  
  11. $language = array(
  12.  
  13.     "",
  14.  
  15.     "English",
  16.  
  17.     "Portuguese",
  18.  
  19.     "Latvian",
  20.  
  21.     "Spanish");
  22.  
  23. for ($i = 1; $i <= 4; $i++)
  24.  
  25. {
  26.  
  27. print "<option value=$i>$language[$i]</option>";
  28.  
  29. print "<h3>Language is : $language[$i]</h3>";
  30.  
  31. }
  32.  
  33. ?>
  34.  
  35. </select>
I am not sure I need all of this and also don't know how to redirect the user to the right page with the above script. Obviously, I could have written individual pages for the original redirect but that was far too easy and I wouldn't have learned nearly as much as I have this week!

I hope someone can help before I go completely mad!
Simon
Oct 22 '06 #1
Share this Question
Share on Google+
2 Replies


ronverdonk
Expert 2.5K+
P: 4,258
I hope that you have some typo's in your code, otherwise they are errors.
You intend to use session variables, but nowhere can be seen that the session is started. Where you address the session variables they are spelled $_SESSION['xxx'] instead of $SESSION['xxx'], as in your code.

Another thing: this code looks a bit complicated for such a relatively simple task. Since we cannot see the other code you are using, I cannot deduct if you e.g. have set the variable $lang which you test with if (! isset($lang)) {.

Also it is not clear what the names of the shown code scripts are, what is "select.php" or 'language.php' and where is the remaining code for the last snippet?

Not very much help I'afraid, but, if you want help, you better show some more of the code with the script names.

Ronald :cool:
Oct 24 '06 #2

P: 2
I hope that you have some typo's in your code, otherwise they are errors.
You intend to use session variables, but nowhere can be seen that the session is started. Where you address the session variables they are spelled $_SESSION['xxx'] instead of $SESSION['xxx'], as in your code.

Another thing: this code looks a bit complicated for such a relatively simple task. Since we cannot see the other code you are using, I cannot deduct if you e.g. have set the variable $lang which you test with if (! isset($lang)) {.

Also it is not clear what the names of the shown code scripts are, what is "select.php" or 'language.php' and where is the remaining code for the last snippet?

Not very much help I'afraid, but, if you want help, you better show some more of the code with the script names.

Ronald :cool:
Thanks Ronald, you have helped more than you think.

I have changed the code which may not be perfect, must be better!

My file tree is:
tree

As you can see, I now have the following main files: index.php and select.php
Working versions at: index.php select.php

They both work fine as they are. Index.php displays the correct language page, calling up the appropriate content from the language folders. select.php, displays the content according to the language selected.
What I want to do, is merge the code somehow so that the user initially arrives at the page apropriate to their browser as in index.php but has the option of changing their language preference as in select.php. I like the 'on select' method used here as it keeps the interface clean.

The code I have so far is:
index.php
Expand|Select|Wrap|Line Numbers
  1. <?
  2. class object {};
  3. $config = new object;
  4. $config->dir     = "/home_path/lang12/lang_sets/";
  5. session_start();
  6. if (! isset($lang)) {
  7.     $lang = "en";
  8.     $lang = "pt";
  9.     $lang = "sp";
  10.     $lang = "lv";
  11. }
  12. else if ($lang == "pt-br" ) { $_SESSION["lang"] = "pt"; }
  13. else if ($lang == "en" ) { $_SESSION["lang"] = "en"; }
  14. else if ($lang == "sp" ) { $_SESSION["lang"] = "sp"; }
  15. else if ($lang == "lv" ) { $_SESSION["lang"] = "lv"; }
  16. else { $_SESSION["lang"] = "en"; }
  17. $config->global = $config->dir.$lang."/language.php";
  18. include ($config->global);
  19. session_destroy();
  20. ?>
  21.  
  22.  
  23. <?=$content1?>
  24. <?=$content3?>
  25. <?=$content2?>
  26. <?=$content4?>
select.php:
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $lang = $_GET["language"]; 
  3. if ($lang=="") {
  4.     $lang = 1; 
  5. }
  6. ?> 
  7. <select onchange="window.location='select.php?language='+this.value"> 
  8. <?php 
  9.  
  10. $language = array(
  11.  
  12.     "",
  13.  
  14.     "English",
  15.  
  16.     "Portuguese",
  17.  
  18.     "Spanish",
  19.  
  20.     "Latvian");
  21.  
  22. for ($i = 1; $i <= 4; $i++)
  23.  
  24. {
  25.  
  26. ?>
  27.  
  28. <option value="<?=$i?>" <? if ($lang == $i) { print "SELECTED";}?>> <?=$language[$i]?></option>
  29.  
  30. <?
  31.  
  32. }
  33.  
  34. ?>
  35.  
  36. </select>
  37. <!-- content here -  Just to show it works! -->
  38. <br>
  39.  
  40. <img src = " <?=$lang?>.gif"><!--posibly a flag here?-->
  41. <?
  42. include("./$lang.htm");
  43. ?><br />
  44. <? if ($lang==1) { ?>
  45. English
  46. <? }
  47. elseif ($lang==2) { ?>
  48. Portuguese
  49. <? }
  50. elseif ($lang==3) { ?>
  51. Spanish
  52. <? }
  53. elseif ($lang==4) { ?>
  54. Latvian
  55. <? }
  56. else ($lang==1) ?>
I think this way would make updating and adding new files very easy as I could use a similar method for all the other pages including language specific menus and content from the other pages (I think)

The typical content for the language.php file could be:
lang_sets/lv/language.php
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>webwhiz lang12 lv</title>
  6.  
  7. </head>
  8.  
  9. <body>
  10. <?
  11. $content3 = "text";
  12. $content2 = " in Latvian";
  13.  
  14. $content4 = include_once("../include/footer.php");
  15. ?>
  16. </body>
  17. </html>
I hope you can see what I am aiming at here. It will keep file organisation simple and allow additional languages to be added easily as some of the code could be inserted as 'include' files.

I a sure that once you give me the answer for this step, I will hit more walls but I am learning all the time :-)

Thanks for your help so far
Simon
Oct 25 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.