0
Good afternoon, I would like to clear up a question I have for some time in PHP.
For example, in this case, I have the site’s translation system, but it only works for the directory in which it is, that is, it only works if it is included in pages in a directory, if it is in a subdirectory, would have to make a new translation system file with the links starting at ../
.
Is there a better solution to this problem, such as a link raiz/lang/pt.php
that worked on any page in any directory. I’ve tried $_SERVER['DOCUMENT_ROOT']
but it doesn’t seem to work right on the host location, brings me the location fo file (c:/xaamp/htdocs/).
<?php
//Detecao se não existir cookie
if(!isset($_COOKIE["lang"])){
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "pt":
//echo "PAGE PT";
$expire=time()+60*60*24*30;
setcookie("lang", "pt", $expire);
$_COOKIE["lang"] = "pt";
break;
case "en":
//echo "PAGE EN";
$expire=time()+60*60*24*30;
setcookie("lang", "en", $expire);
$_COOKIE["lang"] = "en";
break;
default:
//echo "PAGE EN - Setting Default";
$expire=time()+60*60*24*30;
setcookie("lang", "en", $expire);
$_COOKIE["lang"] = "en";
break;
}
}
//Alteração linguagem por link
if(isset($_GET["lang"])){
$lang = $_GET["lang"];
switch ($lang){
case "pt":
//echo "PAGE PT";
$expire=time()+60*60*24*30;
setcookie("lang", "pt", $expire);
$_COOKIE["lang"] = "pt";
break;
case "en":
//echo "PAGE EN";
$expire=time()+60*60*24*30;
setcookie("lang", "en", $expire);
$_COOKIE["lang"] = "en";
break;
default:
//echo "PAGE EN - Setting Default";
$expire=time()+60*60*24*30;
setcookie("lang", "en", $expire);
$_COOKIE["lang"] = "en";
break;
}
}
// No caso de existir cookie
$lang = $_COOKIE["lang"];
switch ($lang){
case "pt":
//echo "PAGE PT";
//include
require_once("lang/pt.php");
break;
case "en":
//echo "PAGE EN";
//include
require_once("lang/en.php");
break;
default:
//echo "PAGE EN - Setting Default";
//include
require_once("lang/en.php");
break;
}
?>`