views:

114

answers:

2

Some people have told me that the following code is bad for HTML validation:

index.php:

line 1 ~ 5 (this one is OK. Just for reference):

<?php include_once 'localization.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html>
<head>

line 15 (if Chinese is selected load those CSS files):

<?php if($lang_file=='lang.zh-tw.php' || $lang_file=='lang.zh-cn.php') 
{echo '<link rel="stylesheet" type="text/css" href="styles/chinese.css" />';} ?>

line 21 (if English is the current language change assign to it the class: current):

<li <?php if($lang_file=='lang.en.php') {echo 'class="current"';} ?>>
<a href="index.php?lang=en">ENGLISH</a></li>

line 168 ~ 171 (this is the only way I found to add different languages to the jquery validation plugin):

<?php if($lang_file=='lang.en.php') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-en.js"></script>';} ?>
<?php if($lang_file=='lang.es.php') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-es.js"></script>';} ?>
<?php if($lang_file=='lang.zh-tw.php') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-zh-tw.js"></script>';} ?>
<?php if($lang_file=='lang.zh-cn.php') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-zh-cn.js"></script>';} ?>

lozalization.php (for reference only):

<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang'])) {
    $lang = $_GET['lang'];

    // register the session and set the cookie
    $_SESSION['lang'] = $lang;
    setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang'])) {
    $lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang'])) {
    $lang = $_COOKIE['lang'];
}
else {
    $lang = 'en';
}

// use appropiate lang.xx.php file according to the value of the $lang
$languages = array('en', 'es', 'zh-tw', 'zh-cn');
if (in_array($_SESSION['lang'], $languages)) {
    $lang_file = 'lang.'.$_SESSION['lang'].'.php';
} else {
    $lang_file = 'lang.en.php';
}

//localization helper function
function l($localization) {
    global $lang;
    return $lang[$localization];
}
    include_once 'languages/'.$lang_file;
?>

I am falling into bad practices or validation problems? The code works, but is there's a way of doing the things above in a cleaner and better way?

A: 

I can't speak specifically for bad practices, but in terms of loading the correct jquery script, just do a substring match on the contents of $lang_file, then concatenate it onto the end of the directory holding all of the scripts. That way, you can add/remove locales without having to change your script.

DWSR
+1  A: 
Mala
@Mala I worked perfectly thanks. One more thing, why did you include that explode thing?
janoChen
Don't run the code! He's trying to blow up your server! J/k:http://us3.php.net/explodeBTW, install the PHP.net search tool for Firefox. That way you can easily look up php functions.
Lèse majesté
@Lèse majesté Ha you scared me because I didn't know the meaning of j/k.
janoChen
explode turns the string into an array, breaking it up along the argument, in this case '.' - Effectively, what we need is the part of $lang_file between the first two dots. So, first we break it into an array, and then select the second element (index 1)
Mala