現在,我有一個使用兩種語言(法語和英語)的網站。。多語言網站的htaccess技巧
它目前的工作方式是,如果有人去mysite.com/folder/file.php
例如,file.php
僅僅是計算出使用哪種語言的腳本,得到它自己的路徑和文件名(file.php
),並擔任了mysite.com/en/folder/file.php
(如果語言是英語)。但是,在URL中顯示的內容仍然是mysite.com/folder/file.php
。
對於任何文件夾和任何文件,使用相同的腳本。如果我想添加一個新文件,我必須將文件添加到用戶輸入瀏覽器的文件夾以及en
和fr
文件夾中。
我可以做一些.htaccess
技巧,以便無論輸入什麼URL,打開一個.php
文件,檢查所請求的語言和文件夾/文件,然後提供正確的語言文件?
下面是爲URL中的任何文件提供的php文件。
<?php
// Get current document path which is mirrored in the language folders
$docpath = $_SERVER['PHP_SELF'];
// Get current document name (Used when switching languages so that the same
current page is shown when language is changed)
$docname = GetDocName();
//call up lang.php which handles display of appropriate language webpage.
//lang.php uses $docpath and $docname to give out the proper $langfile.
//$docpath/$docname is mirrored in the /lang/en and /lang/fr folders
$langfile = GetDocRoot()."/lang/lang.php";
include("$langfile"); //Call up the proper language file to display
function GetDocRoot()
{
$temp = getenv("SCRIPT_NAME");
$localpath=realpath(basename(getenv("SCRIPT_NAME")));
$localpath=str_replace("\\","/",$localpath);
$docroot=substr($localpath,0, strpos($localpath,$temp));
return $docroot;
}
function GetDocName()
{
$currentFile = $_SERVER["SCRIPT_NAME"];
$parts = Explode('/', $currentFile);
$dn = $parts[count($parts) - 1];
return $dn;
}
?>