使用 PHP 在网站上切换语言
我只是在寻求一些建议。我正在创建一个提供(至少)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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会选择会话变量。
在页面的开头,您将看到:
然后您将看到一些用于更改语言的链接
Changelanguage.php 类似于
I would go for session variable.
At the beginning of your pages you'll have:
Then you'll have some links to change the language
Changelanguage.php simply is something like
您是否考虑过使用
$_SERVER["HTTP_ACCEPT_LANGUAGE"]
?像这样的东西:当然,switch 语句可能更适合这里,并且有更多的方式可以说英语而不仅仅是 en,但这应该可以在用户不需要做任何事情的情况下工作。如果他们手动更改,请按照 Ben 的回答将其存储在 cookie 中。
Have you thought about using
$_SERVER["HTTP_ACCEPT_LANGUAGE"]
? Something like this: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.
最常见的方法是将其用作 url 的一部分,并在页面加载时提取它:
您使用框架吗?
The most common way would be to use it as part of the url and extract it when a page loads:
Are you using a framework?
传递语言标识符的最常见方式是子域。
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