需要两次静止以使语言更改
我正在开发一个简单的着陆页面,您可以在其中选择显示的文本的语言。
这是我的代码:
<?php
$defaultLang = isset($_COOKIE['lang_bcn']) ? $_COOKIE['lang_bcn'] : 'es'; //default
if(isset($_GET['selectedLanguage'])){
$languageOption = "";
switch($_GET['selectedLanguage']){
case 'en':
$languageOption = 'en';
break;
case 'es':
$languageOption = 'es';
break;
default:
break;
}
if(isset($languageOption)){
setcookie('lang_bcn',$languageOption,time()+24*7*60*60);//set cookie to expire in 7 days
}
}
$arrayLang['en']['subtitle'] = 'Discover and learn by playing';
$arrayLang['es']['subtitle'] = 'Descubre y aprende jugando';
?>
//links to language selection
<a href="?selectedLanguage=es">ES</a>
<a href="?selectedLanguage=en">EN</a>
//showing the text in the selected language
<p><?php echo $arrayLang[$defaultLang]['subtitle'];?></p>
它的效果很好,除了更改所选语言链接上两次我必须单击的语言。只需单击文本的语言,文本不会改变,我找不到原因。
有人可以帮我吗?
谢谢你!
I am developing a simple landing page where you can choose the language of the text displayed.
This is my code:
<?php
$defaultLang = isset($_COOKIE['lang_bcn']) ? $_COOKIE['lang_bcn'] : 'es'; //default
if(isset($_GET['selectedLanguage'])){
$languageOption = "";
switch($_GET['selectedLanguage']){
case 'en':
$languageOption = 'en';
break;
case 'es':
$languageOption = 'es';
break;
default:
break;
}
if(isset($languageOption)){
setcookie('lang_bcn',$languageOption,time()+24*7*60*60);//set cookie to expire in 7 days
}
}
$arrayLang['en']['subtitle'] = 'Discover and learn by playing';
$arrayLang['es']['subtitle'] = 'Descubre y aprende jugando';
?>
//links to language selection
<a href="?selectedLanguage=es">ES</a>
<a href="?selectedLanguage=en">EN</a>
//showing the text in the selected language
<p><?php echo $arrayLang[$defaultLang]['subtitle'];?></p>
And it works great, except that to change the language I have to click twice on the chosen language link. With just one click the language of the text doesn't change and I can't find the reason why.
Can someone help me?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
$ defaultlang
变量在顶部定义,这取决于cookie。第一次单击按钮时,cookie尚未设置,因此$ defaultlang
将为'es'
,然后将其设置为cookie。第二次单击按钮cookie已经设置了,因此$ defaultlang
变量设置为正确的值。只需将其放在语句的情况下,就应该使用。Your
$defaultLang
variable is defined on the top and it depends on the cookies. When you click your button the first time, the cookies aren't set yet, so$defaultLang
is going to be'es'
and after that you set the cookies. The second time you click your button cookies are already set, so$defaultLang
variable is set to proper value. Just put it belowif
statement and it should work.