php - Simple multi-lingual page system

372

I wanna make my website in 3 languages. I made simple system for change language.

PAGE

//define default lang
<div id="sitelanguage"><?php include "lang/pl.php"; ?></div>

// example of using lang
<li class="menu_glowne"><?php echo $text["menu_naszemarki"]; ?></li>

Language file (example pl.php)

<?php
$text["menu_manifest"] = "MANIFEST";
$text["menu_naszemarki"] = "NASZE MARKI";
$text["menu_wspolpraca"] = "WSPÓŁPRACA";
$text["menu_kontakt"] = "KONTAKT";

?>

And simple jQuery code that will change language to en.php

$('#langen').click(function () {

$('#sitelanguage').html('<?php include "lang/en.php"; ?>');
location.reload();

});

And now, its time for question. Why this doesnt work? ;)

233

Answer

Solution:

PHP is executed on the server, the javascript is executed on the client, so you need to do the site-language server-side, for example by setting a cookie if the user selects another language than the default. Also, always use English as a base language.

<a href="?lang=english">English</a> <a href="?lang=polish">Polish</a> <a href="?lang=danish">Danish</a>

Then include "lang.php" in all your files and have lang.php contain something like

if( !empty( $_GET['language'] ) )
{
  $language = $_GET['language']
  setcookie("language", $language, time()+3600*24*365);
} else 
{
  $language = (empty($_COOKIE['language']))?'english':$_COOKIE['language'];
}

switch( $language )
{
  case "english":
    include "language/english.php";
  break;
  case "polish":
    include "language/polish.php";
  break;
  case "danish":
    include "language/danish.php";
  break;
}

People are also looking for solutions to the problem: php - how can i get a temp table result from a string?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.