Topic: please help with lang file...

Hi all;

I'm new to php , at the moment i'm trying to make a website which visitor can switch the language on the fly ( no need database) and in the same site(page)..like punbb; the user can choose the language by using lang files.

Here is my script for index.php (main page)

<?php
session_start();

if(!isset($lang)) {
    $lang = getenv('HTTP_ACCEPT_LANGUAGE');
    $lang_default = "en";
}

if (is_file("lang/index.$lang.php"))
{
    $_SESSION["lang"] = $lang;
    include("lang/index.$lang.php");
} else {
    $_SESSION["lang"] = $lang_default;
    include("lang/index.$lang_default.php");
}

?>    
<body>
<form action="<?php echo $PHP_SELF;?>" method="get">
<select name="lang" size="1" onchange="this.form.submit()">
 <option value="en">English</option>
<option value="cn">France</option>
        </select>
</form>    


<p><?PHP echo main;?></p>


</body>

and here is my index.en.php

<?PHP
define("main", "How it works?");

?>

but the problem is i cant switch it; it always is at the default lang; i cant switch to others. But if i put the form and the script away from the main page. Let the user choose language frist then can access to the main page then it can work.

Can you guys please help to show how i can get it works inside the web page. So they can always switch the language at the page they are at.

Please help

Thank you very much

I love sky - i love flowers - and i love myself :D

2 (edited by dmspilot00 2007-04-07 21:38)

Re: please help with lang file...

Is $lang set correctly? Is "register globals" enabled on your PHP setup? Try adding the following to the bottom of your page:

<?php echo "The language you selected is $lang"; ?>

FYI It is recommended you NOT use register globals for security reasons.

Re: please help with lang file...

No I do not turn on register globals.

Now it only appears the default lang but i can not switch to the other languages...

I did try your code and it does show

vi,en-us;q=0.9,zh-cn;q=0.8,zh-hk;q=0.6,zh-mo;q=0.5,zh-sg;q=0.4,zh-tw;q=0.3,zh;q=0.1

Please help.

Thank you very much

I love sky - i love flowers - and i love myself :D

Re: please help with lang file...

but furthermore

are my codes above right? because i'm not sure it is right or not...

Thanks

I love sky - i love flowers - and i love myself :D

Re: please help with lang file...

Then you need to get the form variable first.  Add the following to the beginning of your index.php:

$lang = $_GET['lang'];

6 (edited by mystic 2007-04-08 00:10)

Re: please help with lang file...

omg i have it works!!!

thanks a lot

u r my man <3

but the last question please..cause i want it can translate to Chinese as well. But the problem is they will have to have different Charset (Encode) ; so what should i do?

should i add like this ?

if ($lang = cn)
{
$charset = 'GB2312';
}

thank you very much

I love sky - i love flowers - and i love myself :D