.htaccess trickery multi-language website
- by user1658741
I have a website right now that uses two languages (french and english) The way it works right now is that if someone goes to mysite.com/folder/file.php for example, file.php is simply a script that figures out which language to use, get's it's own path and filename(file.php) and serves up mysite.com/en/folder/file.php (if the language is english). However what shows up in the URL is still mysite.com/folder/file.php.
For any folder and any file the same script is used. If I want to add a new file I have to add the file to the folder the user types into the browser as well to the en and fr folders. Could I do some .htaccess trickery so that whatever URL is typed, one .php file gets open that checks the language and what folder/file was requested and then serves up the correct language file?
here's the php file that is served up for any files in the URL.
<?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;
}
?>