0

Hello,
I try to explain my situation. I need to insert multilanguage support in my php site. I used this script called common.php:

<?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';
}

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;
?>

After I create a folder with translation file: lang.en.php, lang.de.php and lang.es.php in this mode for each language:

<?php
/* 
------------------
Language: English
------------------
*/

$lang = array();

$lang['PAGE_TITLE'] = 'My website page title';
$lang['HEADER_TITLE'] = 'My website header title';
....
?>

Now I include common.php (ex. <?php include ('common.php'); ?> ) in first line of my header. Header in included in each php page (ex. <?php include ('header.php'); ?> ).

If I try to insert traslation in this mode:

<title><?php echo $lang['PAGE_TITLE']; ?></title>

I view "?" and I don't view correct translation.
Someone can help me to solve this problem?

2
Contributors
2
Replies
12
Views
3 Years
Discussion Span
Last Post by giovannitao_1
0

I view "?" and I don't view correct translation.

What does that mean?

Anyway, you could have a whitelist of languages to check against before any processing:

$defaultLang = "en";
$allowedLangs = array("en","cy","it","es","pt");

if(isset($_GET['lang']) && in_array($_GET['lang'], $allowedLangs))
{
    $lang = $_GET['lang'];
}else{
    header("Location: ?lang=$defaultLang"); // or whatever pattern you use like mod_rewrite
    exit;
}

...do further processing...
0

I mean that instead text I see a question mark "?"
I don't know why. I use this script: Click Here and seems to be go

Edited by giovannitao_1

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.