Jump to content

"Switch User (Language)" Buttons


bramwolf
 Share

Recommended Posts

Hey Guys! I already posted this in Multi-Language but maybe it sits better here, Sorry!

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


Right from the docs. ( Languages are well documented )


echo "<ul>";

// remember what language is set to

$savedLanguage = $user->language;

foreach($languages as $language) {

// if user is already viewing the page in this language, skip it

if($language->id == $savedLanguage->id) continue;

// if this page isn't viewable (active) for the language, skip it

if(!$page->viewable($language)) continue;

// set the user's language, so that the $page->url and any other

// fields we access from it will be reflective of the $language

$user->language = $language;

// output a link to this page in the other language

echo "<li><a href='$page->url'>$language->title: $page->title</a></li>";

}

// restore the original language setting

$user->language = $savedLanguage;

echo "</ul>";

Link to comment
Share on other sites

Right from the docs. ( Languages are well documented )
echo "<ul>";

// remember what language is set to
$savedLanguage = $user->language; 

foreach($languages as $language) {

  // if user is already viewing the page in this language, skip it
  if($language->id == $savedLanguage->id) continue; 

  // if this page isn't viewable (active) for the language, skip it
  if(!$page->viewable($language)) continue;

  // set the user's language, so that the $page->url and any other
  // fields we access from it will be reflective of the $language
  $user->language = $language; 

  // output a link to this page in the other language
  echo "<li><a href='$page->url'>$language->title: $page->title</a></li>";
}
// restore the original language setting 
$user->language = $savedLanguage; 

echo "</ul>";

Hi there,

So, I've tried to code above and it does output the links to the page's content in the available different languages,

however when I click the links I keep ending up on the page in the same language I was already in. 

Any ideas on what I am doing wrong? As apparent, I'm not quite familiar with php or getting it to do with

processwire what I want, but it seems to me, from other examples I saw, there should be a ->get($language)

statement in the links somewhere? Or is there something else I'm messing up? 

My languages are setup as following:

Default -> Dutch

de        -> German

en        -> English

thanks in advance :)

Link to comment
Share on other sites

Sorry nothing to help you with your language problems (I didnt setup PW with multi lang so far)

But looking to your code in your first try:

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

to send a hidden value you better should use:

<input type="hidden" name="your_field" value="your_value" >

The next thing, if you send only one Variable to get some new content, you could use an simple <a> like this:

<a href="<?= $page->url ?>?my_var=my_value">Click me</a>

This would output a url like this: 'my_site/?my_var=my_value' 

As you could see all after ? is a so called $_GET variable, to create more Vars you just have to add them linke this:

<a href="<?= $page->url ?>?my_var=my_value&new_var=new_val">Click me</a>

Next is your missing input validation. If you ever process values you could get from users, sanitize them. 

You could do this via the PW built-in system called $sanitizer.

But before you do this, try to use the PW GET and POST functions instead of the PHP global vars $_GET and $_POST

The PW way: $input->post->var_name and $input->get->var_name

Example: 

<form method="post" action="./">
  <input type="text" name="content" />
  <input type="submit" value="Send"
</form>

<?php if($input->post->content): ?>

  <p><?= $sanitizer->text($input->get->content) ?></p>

<?php endif ?>
Link to comment
Share on other sites

Ah!!

Great tips! :) Thanks for taking the time to elaborate on some stuff I did! I'll keep your comments in mind next

time I'll try to write php ( tomorrow I figure :P ) But for now I'm inclined to go with Martijn's code as it already

got me halfway there, and mine didn't :P 

Nontheless,

thanks man! :)

Link to comment
Share on other sites

  • 2 weeks later...

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...