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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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