2013-11-28 83 views
0

我是新來的PHP,我試圖用PHP創建一個簡單的多語言網站。我在下面提到了我的總代碼,並且我在該en.php,fn.php文件中共有四個文件,index.php,setlocal.php和一個文件夾語言環境。時遇到的文件夾結構這樣的..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 
$ 

回答

1

嘗試從該行的文件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 
+0

三江源這麼多,它的工作的罰款。 – vadlamani

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; 

?>