Switching languages on a website with PHP

2020-07-25 10:00发布

I'm just looking for some advice. I'm creating a website that offers (at least) 2 languages. The way I'm setting it up is by using XML files for the language, PHP to retrieve the values in the XML nodes. Say you have any XML file, being loaded as follows:

<?php
$lang = "en";
$xmlFile = simplexml_load_file("$lang/main.xml");
?>

Once the file contents are available, I just output each node into an HTML tag like so:

<li><?php echo $xmlFile->navigation->home; ?></li>
which in turn is equal to : <li><a href="#">Home</a></li>
as a nav bar link.

Now, the way in which I'm switching languages is by changing the value of the "$lang" variable, through a "$_POST", like so:

if(isset($_POST['es'])){
$lang = "es";
}elseif(isset($_POST['en'])){
$lang = "en";
}

The value of the "$lang" variable is reset and the new file is loaded, loading as well all the new nodes from the new XML file, hence changing the language.

I'm just wondering if there is another way to reset the "$lang" variable using something else, other than "$_POST" or "$_GET". I don't want to use query string either. I know I could use JavaScript or jQuery to achieve this, but I'd like to make the site not too dependable on JavaScript.

I'd appreciate any ideas or advice.

Thanks

4条回答
姐就是有狂的资本
2楼-- · 2020-07-25 10:47

The most common way to pass a language identifier is subdomain.

http://en.wikipedia.com/

both subdomains should point to the same directory and actual language can be easily extracted from the HTTP_HOST

and for storing language files the solution is gettext

查看更多
走好不送
3楼-- · 2020-07-25 10:54

I would go for session variable.

At the beginning of your pages you'll have:

 if (!isset($_SESSION['language']))
    $_SESSION['language'] = "en";

Then you'll have some links to change the language

<a href="changelanguage.php?lang=es">Español</a>
<a href="changelanguage.php?lang=fr">Français</a>

Changelanguage.php simply is something like

$language = $_GET['lang'];
// DO SOME CHECK HERE TO ENSURE A CORRECT LANGUAGE HAS BEEN PASSED
// OTHERWISE REVERT TO DEFAULT
$_SESSION['language'] = $language;
header("Location:index.php"); // Or wherever you want to redirect
查看更多
Evening l夕情丶
4楼-- · 2020-07-25 10:55

The most common way would be to use it as part of the url and extract it when a page loads:

http://www.your-site.com/en/somepage

Are you using a framework?

查看更多
时光不老,我们不散
5楼-- · 2020-07-25 10:58

Have you thought about using $_SERVER["HTTP_ACCEPT_LANGUAGE"]? Something like this:

if ($_SERVER["HTTP_ACCEPT_LANGUAGE"]) { 
    $langs = explode(",", $_SERVER["HTTP_ACCEPT_LANGUAGE"]); 
    for ($i = 0; $i < count($langs); $i++) { 
        if ($langs[$i] == "en") { 
            $lang = "en";
            break;
        } 
        elseif($langs[$i] == "es") {
            $lang = "es";
            break;
        }
    } 
}

Of course, a switch statement might fit a bit better here, and there's more ways to say English than only en, but this should work without the user having to do a thing. If they manually change, store it in a cookie as per Ben's answer.

查看更多
登录 后发表回答