views:

305

answers:

6

I have this in my index.php:

<?php include_once 'file.php' ?> then I have <html> some content </html>

and I have this in file.php:

<?php
session_start();
header('Cache-control: private');

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

$_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';
}

switch ($lang) {
  case 'en':
  $lang_file = 'lang.en.php';
  break;

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

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

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

}

include_once 'languages/'.$lang_file;
?>

I have those 3 files, I mean lang.en.php an so on...

and it doesn't work, my page is empty, no text, why? What's wrong?

lang.en.php contains

<?php


$h1 ="HOME";
$h2 ="ABOUT US";
$h3 ="CONTACT";
$h4 ="FAQ";

$txt1 = "Here goes txt,Here goes txt,Here goes txt,Here goes txt,
         Here goes txt,Here goes txt,Here goes txt,Here goes txt,
         Here goes txt, Here goes txt,Here goes txt,Here goes txt.";
?>

I don't know what's wrong with this... What should I change? Can someone help me. Thanks.

+1  A: 

You are not printing anything except some HTML. To see the contents of the strings in your lang.en.php's files, you'll need to output them using an echo language construct.

<?php
echo $txt1;
?>

would output:

Here goes txt,Here goes txt,Here goes txt,Here goes txt,
Here goes txt,Here goes txt,Here goes txt,Here goes txt,
Here goes txt, Here goes txt,Here goes txt,Here goes txt.
Chris Clarke
I have that in index.php, why do I need this in file.php?
bhunter
If you have that in index.php, you don't need it in file.php. The code you've posted works, so its indicating the issue is likely with index.php. Can you please post your index.php file?
Chris Clarke
A: 

I know that, and I have it in index.php on every place that I need it, I mean I have <?php echo txt1; ?>, so I'm not sure what's wrong, but I think that something is wrong with file.php on this line switch ($lang) { case 'en': $lang_file = 'lang.en.php'; break;

bhunter
OK, that's good. It wasn't clear if you had in your original question. If you place, `error_reporting(E_ALL);` at the top of file.php, do you see any errors?
Chris Clarke
I don't see any errors, maybe I don't know where to look for them :-(
bhunter
A: 

Problems like these effectively never occur when you use proper code structuring: indent control structures, haven controllers (logic) and views (presentation). You are mixing up a lot stuff there in one file which makes it obviously difficult for you and others to read and understand the code. Please try to code with readability/maintainability in mind, even if you are using php.

Willi
A: 

Oke, I'm new to this, and all I want is one separate file in this case it's file.php and this file should contain php cookies or session to remember language, so that user when browsing through site, can browse in whatever language he wants. That's it. I dunno how to do that, so I found something on google and it doesn't work for me. Thanks.

bhunter
A: 

session_start() needs to be at the top of your document. the session must start before something is sent to the browser.

Vincent Baert
A: 

Hello. I tested you code (it's from Bitrepository tutprial right :)

Oky, the file.php should be in your function.php if ever you have one.

I assume you had index.php and it echoed those variables from your language files right like:

echo $h1;

echo $h2;

echo $h3;

echo $txt1;

To change language, see your file.php, it's specified in the cases. If you want english:

index.php?lang=en

index.php?lang=de

index.php?lang=es

And so on.

Easy right (^_^)/

Check bitrepository for much detailed tutorial. I also would like to suggest to use define() for your language files.

ex.

define('TITLE', 'This is the title')

To echo:

echo TITLE;

brixter