Jump to content

Localization in PHP


Imoddedu
 Share

Recommended Posts

Hey there! I'm trying to learn how to localize my website with PHP and I'm running into..an annoing error. >.< :) The PHP error is:Notice: Undefined index: lang in C:\Program Files (x86)\EasyPHP-5.3.3.1\www\SearchWiDe\lang.php on line 8here is the code:header.php

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><!-- Meta Information --><?php include ("lang.php"); ?><title>----</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><meta name="Robots" content="index,follow" /><meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" /> <meta name="Keywords" content="----" /><meta name="Description" content="----" /><!-- CSS/JavaScript --><link rel="Shortcut Icon" href="img/logo16.png" /><style type="text/css">@import url("style.css");</style> <script type="text/javascript" src="js/jquery.js"></script></head><body><div id="page"><div id="topnav"><a href="index.php" class="linktop">Search</a><span class="pipetop">|</span><a href="about.php" class="linktop">About</a><span class="pipetop">|</span><a href="faq.php" class="linktop">FAQ</a><span class="pipetop">|</span><a href="privacy.php" class="linktop">Privacy</a><span class="pipetop">|</span><a href="terms.php" class="linktop">Terms</a><span class="pipetop">|</span><a href="help.php" class="linktop">Help</a><span class="pipetop">|</span><a href="news.php" class="linktop">News</a><span class="pipetop">|</span><a href="blog.php" class="linktop">Blog</a><span class="pipetop">|</span><a href="contact.php" class="linktop">Contact</a><span class="pipetop">|</span><span class="pipetop">Follow us:</span><a href=""><img src="img/social/blogger.png" alt="Blogger" height="11" width="11" /></a><a href=""><img src="img/social/facebook.png" alt="Facebook" height="11" width="11" /></a><a href=""><img src="img/social/twitter.png" alt="Twitter" height="11" width="11" /></a><br /><span class="pipetop">Choose Region:</span><a class="langSelect" href="">US<img class="langFlag" src="img/flags/us.png" alt="United States" /></a><span class="pipetop">|</span><a class="langSelect" href="">CA<img class="langFlag" src="img/flags/ca.png" alt="Canada" /></a><span class="pipetop">|</span><a class="langSelect" href="">UK<img class="langFlag" src="img/flags/gb.png" alt="United Kingdom" /></a><span class="pipetop">|</span><a class="langSelect" href="">IE<img class="langFlag" src="img/flags/ie.png" alt="Ireland" /></a><span class="pipetop">|</span><a class="langSelect" href="">AU<img class="langFlag" src="img/flags/au.png" alt="Australia" /></a><span class="pipetop">|</span><a class="langSelect" href="">NZ<img class="langFlag" src="img/flags/nz.png" alt="New Zealand" /></a><!-- Start of Language Chooser --><br /><span class="pipetop">Choose Language:</span><a class="langSelect" href="?lang=en">English</a><span class="pipetop">|</span><a class="langSelect" href="?lang=fr">French</a><span class="pipetop">|</span><a class="langSelect" href="?lang=es">Spanish</a><span class="pipetop">|</span><?php if ($currLang == "en") { echo "<span class='pipetop'><i>You are viewing ---- in English</i></span>";}elseif ($currLang == "fr") {echo "<span class='pipetop'><i>Vous voyez --- en français</i></span>";} elseif ($currLang == "es") {echo "<span class='pipetop'><i>Usted está viendo la --- en español</i></span>";} else { echo "<span class='pipetop'><i>---</i></span>";} ?></div><br /><div id="logo"><img id="imglogo" src="img/64logo.png" alt="-----" /></div>

and lang.php

<?php session_start(); // Start PHP Sessionif ($_SESSION['lang'] == "") { 	$_SESSION['lang'] = "en"; 	$currLang = "en"; 	}	else {	$currLang = $_GET['lang'];	$_SESSION['lang'] = $currLang;} 	switch($currLang) { 	case "en": 	define("CHARSET","ISO-8859-1"); 	define("LANGCODE", "en"); 	break; 	case "fr": 	define("CHARSET","ISO-8859-1"); 	define("LANGCODE", "fr"); 	break; 	case "es": 	define("CHARSET","ISO-8859-1"); 	define("LANGCODE", "es"); 	break; 	default: 	define("CHARSET","ISO-8859-1"); 	define("LANGCODE", "en"); 	break; 	} 	header('Content-type: text/html;charset=”.CHARSET');	header('Content-Language: ”.LANGCODE');?>

According to the error, it has something to do with the $_GET I think.

Link to comment
Share on other sites

The error you're getting is telling you $_GET['lang'] has not been set. It contains no value. Does it show in the URL?
No, it doesn't. I just go straight to the index.phpI don't go to like, index.php?lang=fr for example. I thought the script automatically puts it to "en"? Edited by Imoddedu
Link to comment
Share on other sites

Typing a link like this does not work:

<a class="langSelect" href="?lang=en">English</a>

You have to type in the filename also - like so:

<a class="langSelect" href="index.php?lang=en">English</a>

Link to comment
Share on other sites

That link should work, it's still a relative link. I don't know if it strictly follows the spec, but browsers won't choke on it. Try this:

if (isset($_GET['lang'])) {  $_SESSION['lang'] = $currLang = $_GET['lang'];}elseif (isset($_SESSION['lang'])) {  $currLang = $_SESSION['lang'];}else{  $_SESSION['lang'] = $currLang = 'en';}

Link to comment
Share on other sites

That link should work, it's still a relative link. I don't know if it strictly follows the spec, but browsers won't choke on it.
Did you test it? It doesn't work for me in IE, FF, Chrome, Opera or Safari.
Link to comment
Share on other sites

I haven't tested it, I believe I've seen it before though.
Ah, ok it works now. Looking back at your code I was actually thinking of doing something similar like that to fix the problem!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...