Jump to content

Handy Cookie language setting


Stripy42

Recommended Posts

This isn't really a problem, it works fine but I wanted to donate my noobie level script because I'm proud of it.

Though I would like to make it completely PHP based.

 

The page starts of as English or whatever you want to be default, Clicking on the flag sets a cookie to another language using Javascript which then reloads the page.

 

The language code is set in the variable $lang for whatever PHP to use.

It is mostly Javascript not PHP but I've used PHP wherever javascript is stupidly complicated.

 

This bit goes in the header or a separate file to be included

<?php 
$lang = $_COOKIE["language"]; //this sets the cookies' info into a normal variable

$extrayear = (date('Y')+2); //sticks 2 years extra life on the cookie as it's not exactly sensitive information

echo ("
<script type=\"text/javascript\">
  function saveLang(lang) {
    expDate = \"1/01/" . $extrayear . " 00:00:00\" // expiry date
    document.cookie=\"language=\"+lang+\"; expires=\"+expDate+\";\" //sets cookie

    window.location.reload() // reloads page
  }
</script>
");
?>

 

This bit goes in the "Body" to choose language, it changes message/image depending on the current language.

You could either set these to any other two languages or use a form menu for many languages

<?php 
  if ($lang=="jp") {
    echo ("<a href=\"javascript:saveLang('en')\" >
    <img src=\"../graphics/flag-uk.gif\" width=\"60\" height=\"40\" alt=\"Set Language to English\" border=\"0\" /></a>");
  } else {
    echo ("<a href=\"javascript:saveLang('jp')\" >
    <img src=\"../graphics/flag-jp.gif\" width=\"60\" height=\"40\" alt=\"日本語\" border=\"0\" /></a>");
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/70542-handy-cookie-language-setting/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.