views:

52

answers:

1

The multilingual content echoed with PHP (e.g. line 17) are not displayed at all.

localizatoin.php:

This one works:

<?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
switch ($lang) {
case 'en':
    $lang_file = 'lang.en.php';
    break;

case 'es':
    $lang_file = 'lang.es.php';
    break;

case 'zh-tw':
    $lang_file = 'lang.zh-tw.php';
    break;

case 'zh-cn':
    $lang_file = 'lang.zh-cn.php';
    break;

default:
    $lang_file = 'lang.en.php';
}

//translation helper function
function l($localization) {
    global $lang;
    return $lang[$localization];
}

    include_once 'languages/'.$lang_file;
?>

This one:

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

function get_lang(){
    if(!empty($_GET['lang'])) return $_GET['lang'];
    if(!empty($_SESSION['lang'])) return $_SESSION['lang'];
    if(!empty($_COOKIE['lang'])) return $_COOKIE['lang'];
    return 'en';
}

function set_lang($lang){
    setcookie("lang", $lang, time() + (3600 * 24 * 30));
    $_SESSION['lang'] = $lang;
}

function get_lang_file($lang){
    $lang_file = "languages/lang.$lang.php";
    if(file_exists($lang_file)) return $lang_file;
    if($lang_file = get_lang_file('en')) return $lang_file;
    return false;
}

//translation helper function
function l($string){
    static $localization;

    if(!isset($localization)){
        $lang = get_lang();
        $lang_file = get_lang_file($lang);
        if($lang_file) set_lang($lang);
        $localization = include $lang_file;
    }

    return $localization[$string];
}

?> 

displays the following error:

Notice: Undefined variable: lang_file in D:\wamp\www\test2\localization0.1\index.php on line 17
(and so on)

languages/lang.en.php (sample):

<?php
$lang = array(
    'tagline_p' => "Hello!...",
(continues)

index.php (sample):

    <?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>
    <meta name="robots" content="index, follow"/>
    <meta name="description" content="Web design and Translation / 網頁設計和翻譯" />
    <meta name="keywords" content="web development, web developer, web design, web designer, translation, translator, taiwan, taipei, taichung, english, chinese, spanish, 網站開發者, 網頁設計, 網頁設計師, 翻譯, 翻譯著, 台灣, 台北, 台中, 英文, 中文, 西班牙文, html, css, javascript, php" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
    <title>Alex Chen - Web design and Translation / 網頁設計和翻譯</title>
    <link rel="stylesheet" type="text/css" href="styles/reset.css" />
    <link rel="stylesheet" type="text/css" href="styles/global.css" />
    <link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.3.1.css" />
    <?php if(get_lang() == 'zh-tw' || 'zh-cn') {echo '<link rel="stylesheet" type="text/css" href="styles/chinese.css" />';} ?>
(continues)

line 17:

<?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 23 to 26:

<ul id="lang">
        <li <?php if($lang_file=='lang.en.php') {echo 'class="current"';} ?>><a href="index.php?lang=en">ENGLISH</a></li>
        <li <?php if($lang_file=='lang.es.php') {echo 'class="current"';} ?>><a href="index.php?lang=es">ESPAÑOL</a></li>
        <li <?php if($lang_file=='lang.zh-tw.php') {echo 'class="current"';} ?>><a href="index.php?lang=zh-tw">中文(繁體)</a></li>
        <li <?php if($lang_file=='lang.zh-cn.php') {echo 'class="current"';} ?>><a href="index.php?lang=zh-cn">中文(简体)</a></li>
+2  A: 

Don't be an inappropriately lazy programmer!

$lang = get_lang();
if($lang == 'zh-tw' || $lang == 'zh-cn')
{
 ...
}

Edit: Explanation of the code.

This is simply line 17, expanded into multiple lines. Unless the if statement is truly simple, it is best to avoid single line if statements. This is easier to read.

This assigns the return value of get_lang() to a variable:

$lang = get_lang();

This tests if that lang variable contains either 'zh-tw' or 'zh-ch':

if($lang == 'zh-tw' || $lang == 'zh-cn')

It's late and I'm tired, but I don't think this actually does much of anything:

if(get_lang() == 'zh-tw' || 'zh-cn')

It evaluates to true, but that's because of the way PHP evaluates types in a boolean context. Those are strings and they're not empty. They evaluate to true. The return value of get_lang() is tossed away by PHP and not used, as it's not being assigned to a variable.

George Marian
@George Marian Haha calm down man, I'm not a PHP programmer, I'm just a designer. Can you explain the code you just wrote?
janoChen
@janoChen Oh, I'm calm. That exclamation point was meant to drive the point home, not convey my state of mind. :) Explanation incoming, in the answer.
George Marian