php多语言网站错误问题

php多语言网站错误问题,php,Php,我是php新手,我试图用php创建一个简单的多语言网站。下面我提到了我的全部代码,我总共有四个文件,index.php,setlocal.php,还有一个文件夹区域设置在en.php,fn.php文件中。我有这样的文件夹结构 ---- index.php setlocal.php locale en.php fn.php 文件代码是 index.php <?php include_once "setlocal.php"; ?> <!

我是php新手,我试图用php创建一个简单的多语言网站。下面我提到了我的全部代码,我总共有四个文件,index.php,setlocal.php,还有一个文件夹区域设置在en.php,fn.php文件中。我有这样的文件夹结构

----  
index.php  
setlocal.php  

locale  
 en.php  
 fn.php  
文件代码是

index.php

<?php  
include_once "setlocal.php";  
?>  

<!doctype html>  
<head>  
<title><?php echo $GLOBALS['l']['title1']; ?></title>  
</head>  
<body>  

<ul class="header-nav pull-right">  
<li><a href="index.php?lang=en">English</a></li>  
<li><a href="index.php?lang=fn">French</a></li>  
</ul>  

<p><?php echo $GLOBALS['l']['homdes1']; ?></p>  

</body>  
</html>  
<?php  

($language = @$_GET['lang']) or $language = 'en';  

$allowed = array('en', 'te');  

if(!in_array($language, $allowed)) {  
$language = 'en';  
}  

include "locale/{$language}.php";  

$GLOBALS['l'] = '$local';  

?>  
<?php  

$local = array (  
'title1' => 'sample english content',  
'homdes1' => 'sample english contentsample english contentsample english content');  

?>  
<?php  

$local = array (  
'title1' => 'sample french content',  
'homdes1' => 'sample french contentsample french contentsample french contentsample       french contentsample french content');  

?>  

尝试从文件
setlocal.php
的此行中删除
$local
周围的引号:

$GLOBALS['l'] = '$local'; 
所以它是这样写的:

$GLOBALS['l'] = $local; 
这将为您提供:

// you can use var_dmp() this to see what you have in $GLOBALS['l']
var_dump($GLOBALS['l']);

array(2) {
  ["title1"]=>
  string(22) "sample english content"
  ["homdes1"]=>
  string(66) "sample english contentsample english contentsample english content"
}
然后,可以使用数组语法访问:

echo $GLOBALS['l']['homdes1'];

// gives:    
sample english contentsample english contentsample english content

将setlocal.php更改为:

<?php  

//Check if $_GET['lang'] is set, if not default to 'en'
$language = isset($_GET['lang']) ? $_GET['lang'] : 'en';

$allowed = array('en', 'te');  

if(!in_array($language, $allowed)) {  
  $language = 'en';  
}  

include "locale/$language.php";  

//without single quotes. PHP will expand (convert) variable names inside double quotes to the value, but within single vars are printed as is.
$GLOBALS['l'] = $local; 

?>  

<?php  

//Check if $_GET['lang'] is set, if not default to 'en'
$language = isset($_GET['lang']) ? $_GET['lang'] : 'en';

$allowed = array('en', 'te');  

if(!in_array($language, $allowed)) {  
  $language = 'en';  
}  

include "locale/$language.php";  

//without single quotes. PHP will expand (convert) variable names inside double quotes to the value, but within single vars are printed as is.
$GLOBALS['l'] = $local; 

?>