Jump to content

Switch language buttons


bramwolf
 Share

Recommended Posts

Hey Guys!

I must admit I'm fairly new to all this so I might be overlooking something completely or just not getting it :P

But here's what I want to do! :)

I'm building a website which I want to present in 3 languages. The user should be able to choose his or

her language via the use of a button. I created 3 image buttons that send a value to a php script at the bottom

of my page, but the page keeps rendering in the default language. Any ideas on what I am doing wrong, or

how you would go about achieving this? I would like the user(language) to stay the same as the chosen though

the buttons until it is changed again.

This is what I have so far :

<form action="" method="post">
<input type="text" name="taal" value="nl" style="display:none;">
<input type="image" src="../site/templates/img/vlag_nl.png" name="submit" /> 
</form>
 
<form action="" method="post">
<input type="text" name="taal" value="de" style="display:none;">
<input type="image" src="../site/templates/img/vlag_de.png" name="submit" /> 
</form>
 
<form action="" method="post">
<input type="text" name="taal" value="en" style="display:none;">
<input type="image" src="../site/templates/img/vlag_en.png" name="submit" /> 
</form>
 
<?PHP 
 
if(isset($_POST["submit"])) {
$taal = $_REQUEST['taal'];  
$user->language = $languages->get($taal);
echo $pages->LocalUrl($taal)->render();
};
 
?>
 
 
Any help would be much appreciated!
I've been spending waaay too much time on this piece of hocus pocus already :P
 
Gr,
Bram
Link to comment
Share on other sites

I didnt try it out, but i'm quite sure, that's not the way you're supposed to use HTML for forms ;) (i guess it always targets the first <form> tag)

you might want to try this with a differnt approach, like using checkboxes

<form action="<?php echo $_SEVER['PHP_SELF'] ; ?>" method="POST">
     <label><input type="radio" value="nl" name="language"> NL</label>
     <label><input type="radio" value="de" name="language"> DE</label>
     <label><input type="radio" value="en" name="language"> EN</label>
<input type="submit" value="Ok" />
</form>

and then do the rest of your php stuff.

OR

what i'd rather suggest is a selection via <a> links (way easier usability) like this

<ul id='languageSelection'>
<?php
          foreach($languages as $language) {
                if(!$page->viewable($language)) continue; // check if page not published for this language
                
                $class = "$language" == "$user->language" ? "active" : "";
                $url = $page->localUrl($language);
                $lang = $language->name;
                   if(strcmp($lang,'default') == 0) { $lang = 'nl'; } // default language fix
                echo "<li class='$class'><a title='$language->title' href='$url'>$lang</a></li>";
          }
</ul>

what it does is pretty simple: creating and populating an <ul> element with <li> children and clickable Language-Names (Text e.g. "Deutsch", "English" ,...). You could also insert your <img> Tags into the echo command instead of the $lang Variable.

This Version also adds a "active" Class to Active-Language-<li>, so you're free to customize it with css

best

Link to comment
Share on other sites

  • 2 weeks later...

Hey Overoon,

Thank you so much for your reply! Your script indeed sounds alot better than mine :) So I decided to put it to the test but when I implement it in

my HTML the site gives me a internal error. I've checked the script and the php lines are flawless, no broken lines or whatever. Any ideas on

what could be the problem? I'll try to find the server log and see if it gives any usefull info.

thanks :)

Bram 

Link to comment
Share on other sites

Well this is a problem as it should say $_SERVER on that first line and not SEVER (a bit severe to be severing things if you ask me):

<?php echo $_SERVER['PHP_SELF'] ; ?>

Though I would personally do this instead:

<?php echo $page->url; ?>
  • Like 1
Link to comment
Share on other sites

His is the one I was talking about - read his first line of code and re-read my post :)


EDIT: specifically his first line of code should say $_SERVER and not $_SEVER in it.

Link to comment
Share on other sites

Ah yes! :)

I see what you mean! Quite severe indeed ;) But i actually decided to use this one, since it only lists the other available languages, much better than hardcoding

links to all languages i think! 

<ul id='languageSelection'>
<?php
         
foreach($languages as $language) {
                if(!$page->viewable($language)) continue; // check if page not published for this language
                
$class
= "$language" == "$user->language" ? "active" : "";
                $url = $page->localUrl($language);
                $lang = $language->name;
                   if(strcmp($lang,'default') == 0) { $lang = 'nl'; } // default language fix
                echo "<li class='$class'><a title='$language->title' href='$url'>$lang</a></li>";
          }
?>

</ul>

If this wouldn't give me a internal server error that is :P

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...