0
votes

Given a simple static website with only a homepage and two other pages, each containing text in two langages: English and Dutch. All contents should be held in a single php file per page:

<body>
<menu id="EN">
  <ul>
    <li>About</li>
    <li>Home</li>
  </ul>
</menu>

<menu id="NL">
  <ul>
    <li>Over</li>
    <li>Thuis</li>
  </ul>
</menu>

<article id="EN">
     Goodmorning [...]
</article>

<article id="NL">
     Goedemorgen [...]
</article>
</body>

The default can be English, so upon arrival English content is shown, unless the viewer chooses the Dutch language. Once a user clicks on a language, the system should "remember" that choice sothat further links open in the correct language. How to set this via php/css as simple as possible with a few lines code as possible? If possible at all? This would be the absolute and ultimate alternative and simplest way to have a multilingual website without setting up an entire (database driven dynamic) multilingual framework, way surpassing the modest nature of this wish).

3
Dont repeat the html in a onefile application. Bind language data to named variables and use them in the html. $about = $langarray['DE']['ABOUT']; <li>$about</li>JustOnUnderMillions
The rest of your question is to wide to answer. How about google: make a simple multilingual wepage based on cookies and sessions.JustOnUnderMillions
You can remember the users choise by storing the chosen language in a cookie which can be set to be remembered for a long as you wantSuperDJ

3 Answers

2
votes

The question doesn't entirely make sense. If you're using PHP, you're looking to keep track of the user's language on the server side. This is typically done through session variables.

The alternative is to send both languages and use css and javascript to replace the values on the client side depending on which flag is clicked.

1
votes

This is the simplest implementation of multilanguage based on your information. There are a lot of other better solutions.

You first need a language selector

<div class="language-selector">
    <a href="changelanguage.php?lang=en">EN</a>
    <a href="changelanguage.php?lang=nl">NL</a>
</div>

changelanguage.php contents:

<?php
    session_start();
    if(!empty($_GET['lang']) $_SESSION['lang'] = $_GET['lang'];
    header("Location: ".$_SERVER['HTTP_REFERER'])
?>

Every page should contain on the top

<style type="text/css">
#EN, #NL { display:none; }
<?php
    session_start();
    if($_SESSION['lang'] == "EN" || empty($_SESSION['lang'])) echo "#EN { display: block; }";
    if($_SESSION['lang'] == "NL") echo "#NL { display: block; }";
?>
</style>
1
votes

First mistake is you cannot repeat id attribute in different elements. I think the most simple without CSS is use $_GET["lang"] param, something like this:

In your first line of PHP put this:

<?php 
   if(isset($_GET["lang"]){
      $lang = $_GET["lang"];
   }else{
      $lang = "NL"; // by default
   }
?>

Dutch

 <?php if ($lang=="NL"){ ?>
<article>
     Goedemorgen [...]
</article>
<?php } ?>

English

 <?php if ($lang=="EN"){ ?>
<article>
     Goodmorning [...]
</article>
<?php } ?>

If you want change language:

<a href="?lang=EN>Website in English</a>