Language redirect

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scar5308
    New Member
    • Oct 2006
    • 2

    Language redirect

    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:
    Code:
    <?
    class object {};
    $config = new object;
    $config->dir     = "/home/path/public_html/language2/lang_sets/";
    if (! isset($lang)) {
    	$lang = "pt";
    	$lang = "en";
    	$lang = "lv";
    }
    else if ($lang == "pt-br" ) { $SESSION["lang"] = "pt"; }
    else if ($lang == "en" ) { $SESSION["lang"] = "en"; }
    else if ($lang == "lv" ) { $SESSION["lang"] = "lv"; }
    else { $SESSION["lang"] = "en"; }
    $config->global = $config->dir.$lang."/language.php";
    include ($config->global);
    ?>
    <html>
    <head>
    <title>webwhiz language2</title></head>
    <body>
    <?=$content4?>
    <?=$content1?>
    <?=$content2?>
    </body>
    </html>
    The $content variables can contain php includes contained in the individual language_sets language.php files:

    Code:
    <?
    $content1 = include_once("../include/langselect.php");
    $content2 = include_once("../include/contactform.php");
    $content4 = include_once("../include/footer.php");
    ?>
    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:

    Code:
    <?php
    
    $language = $_GET["language"];
    
    ?>
    
    <select onchange="window.location='select.php?language='+this.value">
    
    <?php
    
    $language = array(
    
        "",
    
        "English",
    
        "Portuguese",
    
        "Latvian",
    
        "Spanish");
    
    for ($i = 1; $i <= 4; $i++)
    
    {
    
    print "<option value=$i>$language[$i]</option>";
    
    print "<h3>Language is : $language[$i]</h3>";
    
    }
    
    ?>
    
    </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
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #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:

    Comment

    • scar5308
      New Member
      • Oct 2006
      • 2

      #3
      Originally posted by ronverdonk
      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
      Code:
      <?
      class object {};
      $config = new object;
      $config->dir     = "/home_path/lang12/lang_sets/";
      session_start();
      if (! isset($lang)) {
      	$lang = "en";
      	$lang = "pt";
      	$lang = "sp";
      	$lang = "lv";
      }
      else if ($lang == "pt-br" ) { $_SESSION["lang"] = "pt"; }
      else if ($lang == "en" ) { $_SESSION["lang"] = "en"; }
      else if ($lang == "sp" ) { $_SESSION["lang"] = "sp"; }
      else if ($lang == "lv" ) { $_SESSION["lang"] = "lv"; }
      else { $_SESSION["lang"] = "en"; }
      $config->global = $config->dir.$lang."/language.php";
      include ($config->global);
      session_destroy();
      ?>
      
      
      <?=$content1?>
      <?=$content3?>
      <?=$content2?>
      <?=$content4?>
      select.php:
      Code:
      <?php 
      $lang = $_GET["language"]; 
      if ($lang=="") {
          $lang = 1; 
      }
      ?> 
      <select onchange="window.location='select.php?language='+this.value"> 
      <?php 
      
      $language = array(
      
          "",
      
          "English",
      
          "Portuguese",
      
          "Spanish",
      
          "Latvian");
      
      for ($i = 1; $i <= 4; $i++)
      
      {
      
      ?>
      
      <option value="<?=$i?>" <? if ($lang == $i) { print "SELECTED";}?>> <?=$language[$i]?></option>
      
      <?
      
      }
      
      ?>
      
      </select>
      <!-- content here -  Just to show it works! -->
      <br>
      
      <img src = " <?=$lang?>.gif"><!--posibly a flag here?-->
      <?
      include("./$lang.htm");
      ?><br />
      <? if ($lang==1) { ?>
      English
      <? }
      elseif ($lang==2) { ?>
      Portuguese
      <? }
      elseif ($lang==3) { ?>
      Spanish
      <? }
      elseif ($lang==4) { ?>
      Latvian
      <? }
      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
      Code:
      <!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=iso-8859-1" />
      <title>webwhiz lang12 lv</title>
      
      </head>
      
      <body>
      <?
      $content3 = "text";
      $content2 = " in Latvian";
      
      $content4 = include_once("../include/footer.php");
      ?>
      </body>
      </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

      Comment

      Working...