繁体   English   中英

PHP多语言网站错误问题

[英]php multilanguage website error issue

我是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>  

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

?>  

地区
|
zh.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  
$  

尝试从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; 

?>  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM