...

View Full Version : two languages. the require does not update ?



likon
08-03-2008, 09:16 AM
function process_choice()
{
$value=$_POST['value'];
$submit=$_POST['submit'];

if(isset($submit))
{
if($value=="en")
{
require ("lib/en.php");
$session=="en";
}else {
require ("lib/id.php");
$session=="id";
}

show_choice();
} else {
show_choice();
}
}

function show_choice()
{
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="value" id="value">';
echo '<label>Select your language: ';
echo '<select name="value">';
echo '<option value="en">English</option>';
echo '<option value="id">french</option>';
echo '</select>';
echo '<input type="submit" name="submit" value="Select" />';
echo '</label>';
echo '</form>';
}



process_choice();


hi guys the problem if .. either you select english or french .

the require does not update.

say all the code above this is hd.php

and index.php require hd.php .which run all the code above

yes . it shows the choice.
but when you submit it . the require ("lib/en.php"); does not update the index.php .

you know what i mean ?

but if you place after the if($value=="en") echo "testetsaetaste";
then the index.php will load echo "testet"

is there anything wrong with the require function as the result of if($value=="en") ?

Fou-Lu
08-03-2008, 10:08 AM
If you enable you're error reporting, it may complain about a non-existant file. This is where include/require gets annoying with PHP. Anything that has been included into another file takes the cwd of the currently executing script, so unless hd.php and index.php are in the same directory, hd.php will be unable to find the files relative to index.php. I'm guessing that hd.php is in a different directory from index.php.

Given the code you have posted, there is absolutely no way to determine if the files have included unless the included files have output. The $session == "en"; and $session == "id"; are probably supposed to be assignments, not comparisons, but it doesn't really make a difference since $session will be crushed on the function stack. If you want to keep it, you need to either return it, globalize it, or pass it into the show_choice call.

Other than that it looks ok. Might want to change you're form name and id so that it doesn't conflict with you're input fields though.

likon
08-03-2008, 10:15 AM
i dont get what you said.
I am a newbie hahahaa

and .. can you show me any example how to make this done ?

the file structure is like this.. hd.php are in the same directory as index.php

project/index.php
project/hd.php
project/lib/en.php
project/lib/fr.php

for example .

and inside index.php is . this

require ("hd.php");
echo $translate['test'];

file in en.php is

$translate=array();
$translate['test']="this is test";


of course the hd. php is all the code in my previous post .

if I just use this code in my index.php
require("lib/en.php");
echo $translate['test'];

it works..

so .. i dont know. i am logical error ?

Fou-Lu
08-03-2008, 08:43 PM
Ok, first we'll need to alter you're code so that it keeps track of what language has been selected. I'm not doing this with cookies, so it will only last as long as the browser is open. Setting a cookie (if the browser will take it) will let you remember what language was last used. Next, we need to alter the scope of the $translate variable. Include/require inherits the scope of where it was called. This means that the $translate array is unable to leave the process_choice function call.


Yes, I changed the if to a switch syntax. This will make expansion easier if you add more languages instead of using an elseif, elseif, elseif...




<?php
session_start();

function process_choice()
{
$value = 'en';
$aResult = null;
if (isset($_POST['value']))
{
$value = $_POST['value'];
}
else if (isset($_SESSION['lang']))
{
$value = $_SESSION['lang'];
}
switch ($value)
{
case 'fr':
require_once './lib/fr.php';
break;
default:
require_once './lib/en.php';
$value = 'en';
}
$_SESSION['lang'] = $value;
$aResult = (isset($translate)) ? $translate : array();
show_choice();
return $aResult;
}

function show_choice()
{
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="frmLang" id="frmLang">';
echo '<label for="selLang">Select your language: </label>';
echo '<select name="value" id="selLang">';
echo '<option value="en"' . ($_SESSION['lang'] == 'en') ? ' selected="selected" : '' . '>English</option>';
echo '<option value="fr"' . ($_SESSION['lang'] == 'fr') ? ' selected="selected" : '' . '>french</option>';
echo '</select>';
echo '<input type="submit" name="submit" value="Select" />';
echo '</form>';
}

$translate = process_choice();

If you need to use the $translate array within the show_choice() function, you'll need to pass it as a function argument instead of globalizing it. It will not exist by this point.
That work?


I think I broke'd something in the selection option values. The syntax highlighting is all off. If you have problems, post back and when I wake up a little more I can fix it for you then, lol.

likon
08-04-2008, 07:16 AM
thanks .

i havent tried it yet. just got home ..


but $translate .. i dont want to make it as process_choice though


ex .. $translate['data']="this sentence will be translated";

so when you echo $translate['data']... the "this sentence will be translated"
should be translated according to the result of "process choice" which is either english / french . .

and i am going to translate line by line..

oh well lemme try out your code .and i will post again .
thanks !



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum