使用按钮更改网站的语言 [英] Change the language of a site using a button

查看:128
本文介绍了使用按钮更改网站的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想按标题的说明使用按钮更改网站的语言,但这无需更改URL mywebsite.com(无需执行mywebsite.com?lang=es),只需更改以下代码中的PHP变量即可:

$xlang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);


if($xlang == "es")
{
$g_lang = "es";
}
else if ($xlang == "en")
{
$g_lang = "en";
}
else
{
$g_lang = "en";
}

然后使用此按钮

<a class="langx" href="#"></a>

在不更改网址的情况下更改语言

有没有办法做到这一点?如果可以,怎么办?

谢谢.

解决方案

单击按钮时,您需要将所需的值发送到服务器,因此您的链接必须具有类似?lang=es的语言字符串. >

然后,您可以在服务器端对其进行处理,并将当前访问者的语言存储在例如数据库(对于已登录用户),会话或cookie中.

然后,您可以使用header重定向将用户重定向到不带语言字符串的站点,并在顶部检查是否已设置db-entry/session/cookie并使用该语言.

Hi I want as the title says change the language of the site using a button but this without altering the url mywebsite.com (Without doing mywebsite.com?lang=es) by just changing PHP variables in this code:

$xlang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);


if($xlang == "es")
{
$g_lang = "es";
}
else if ($xlang == "en")
{
$g_lang = "en";
}
else
{
$g_lang = "en";
}

And then using this button

<a class="langx" href="#"></a>

Change the language without altering the url

Is there a way to do that? if so, how?

Thanks in advance.

解决方案

You need to send the required value to the server when the button is clicked, so your link would have to have the language string like ?lang=es.

Then you can process it on the server side and store the language for the current visitor in for example a database (for logged-in users), a session or a cookie.

Then you can use a header redirect to redirect the user to the site without the language string and check at the top if the db-entry / session / cookie is set and use that language.

这篇关于使用按钮更改网站的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆