使用 PHP 在网站上切换语言

发布于 2024-09-02 04:52:43 字数 848 浏览 6 评论 0原文

我只是在寻求一些建议。我正在创建一个提供(至少)2 种语言的网站。 我设置它的方法是使用 PHP 语言的 XML 文件来检索 XML 节点中的值。 假设您有任何 XML 文件,加载方式如下:

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

一旦文件内容可用,我只需将每个节点输出到 HTML 标记中,如下所示:

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

现在,我切换语言的方式是通过更改“的值” $lang”变量,通过“$_POST”,如下所示:

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

“$lang”变量的值被重置并加载新文件,同时加载新 XML 文件中的所有新节点,从而更改语言。

我只是想知道是否有另一种方法可以使用“$_POST”或“$_GET”之外的其他内容来重置“$lang”变量。我也不想使用查询字符串。 我知道我可以使用 JavaScript 或 jQuery 来实现此目的,但我想让该网站不太依赖 JavaScript。

我将不胜感激任何想法或建议。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我很OK 2024-09-09 04:52:43

我会选择会话变量。

在页面的开头,您将看到:

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

然后您将看到一些用于更改语言的链接

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

Changelanguage.php 类似于

$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

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
鯉魚旗 2024-09-09 04:52:43

您是否考虑过使用$_SERVER["HTTP_ACCEPT_LANGUAGE"]?像这样的东西:

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;
        }
    } 
}

当然,switch 语句可能更适合这里,并且有更多的方式可以说英语而不仅仅是 en,但这应该可以在用户不需要做任何事情的情况下工作。如果他们手动更改,请按照 Ben 的回答将其存储在 cookie 中。

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.

倾听心声的旋律 2024-09-09 04:52:43

最常见的方法是将其用作 url 的一部分,并在页面加载时提取它:

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

您使用框架吗?

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?

柳若烟 2024-09-09 04:52:43

传递语言标识符的最常见方式是子域。

http://en.wikipedia.com/

两个子域应指向同一目录,并且实际语言可以是轻松从 HTTP_HOST 中提取

并存储语言文件,解决方案是 gettext

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文