7

In PHP, I want to change the language (English, German, etc) of the site when clicking a button. Is this the right way to approach that problem?

<?php 
  $language;
  if ($language == "en") {
    include("headerEn.php");
  } else {
    include("header.php");
  } 
?>
<a href="index.php"><?php $language = "en"; ?>
<img src="images/language/languageNO.png"></a>

<a href="index.php"><?php $language = "no"; ?>
<img src="images/language/languageEN.png"></a>

What is the best way to change the language of the site and have it persist when the user returns?

0

7 Answers 7

10

you can do this by

<a href="index.php?language=en">
<a href="index.php?language=no">

and get the languages and store them in cookie and include file according to cookie like

if ( !empty($_GET['language']) ) {
    $_COOKIE['language'] = $_GET['language'] === 'en' ? 'en' : 'nl';
} else {
    $_COOKIE['language'] = 'nl';
}
setcookie('language', $_COOKIE['language']);

and than

if ( $_COOKIE['language'] == "en") {
   include("headerEn.php");
} else {
   include("header.php");
} ?>
Sign up to request clarification or add additional context in comments.

1 Comment

I think the else part should be else if (empty($_COOKIE['language'])), to avoid resetting the language to 'nl' when the cookie (and not the request) contains the choosen language.
3

To give a solution without changing your approach, You can do like this.

<?php 
if(isset($_GET['language']))
  $language = $_GET['language'];
else
  $language = "";

if ($language == "en") {
   include("headerEn.php");
} else {
   include("header.php");
} ?>

<a href="index.php?language = en"><img src="images/language/languageNO.png">      </a>
<a href="index.php?language = no"><img src="images/language/languageEN.png"></a>

If you want to keep the selection, you can store this value in Database or Session.

Comments

3

It is always good to have a default value, so that you never end up being in a no-language site.

$language = $_REQUEST["language"];
$default_header="myheaderXXX.php";

switch ($language) {
    case "en":
      include("headerEn.php");
      break;

    case "no":
      include("header.php");
      break;

    default:
      include($default_header);
}

And then create links like this:

<a href="index.php?language=en">
<a href="index.php?language=no">

Comments

0

You can't change a variable within PHP by html. PHP is serversided HTML is clientsided

You can use GET variables to change it though. It is the easiest way to do it.

Comments

0

You can implement the same code like this . I have editted your code .

<?php 
$language; ?>
<?php if ($language == "en") : ?>
    <?php include("headerEn.php"); ?>
     <a href="index.php"><?php $language = "en"; ?><img src="images/language/languageNO.png"></a> 
<?php else: ?>
    <?php include("header.php"); ?>
     <a href="index.php"><?php $language = "no"; ?><img src="images/language/languageEN.png"></a>
<?php endif; ?> 

this will solve your problem .

Comments

0

Try to save this $language value to session variable. When the page reloaded check that the session variable is set or not.

If set use that $language

NOTE:

$language = $_GET['language'];

Comments

0

In this case I have tried use Session variables to store and define language. Every thing works until i turn on caching. When caching was enabled even reload page returns same language and user could not change language. So I consider to change language using AJAX request after clicking on language button (national flag)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.