php多语言网站错误问题

时间:2013-11-28 01:48:15

标签: php

我是php新手,我试图用php创建一个简单的多语言网站。我的总代码下面提到了,我在en.php,fn.php文件中有四个文件,index.php,setlocal.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>  

setlocal.php

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

?>  

区域设置
     |
    en.php

<?php  

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

?>  

fn.php

<?php  

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

?>  

当我运行代码时,它显示如下所示的错误,请帮我解决这个问题,谢谢。

Warning: Illegal string offset 'homdes1' in  D:\madhu_new\korimerla\htdocs\korimerla\php\index.php on line 15  
$  

2 个答案:

答案 0 :(得分:1)

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

$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

答案 1 :(得分:0)

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

?>