更改网站的语言与HTML按钮 [英] Change language of site with an html button

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

问题描述

在PHP中,我想点击一个按钮,当改变网站的语言(英语,德语等)。这是解决这个问题的正确方法?

In PHP, I want to change the language (English, German, etc) of the site when clicking a button. Is this the right way to approach that problem?

<?php 
  $language;
  if ($language == "en") {
    include("headerEn.php");
  } else {
    include("header.php");
  } 
?>
<a href="index.php"><?php $language = "en"; ?>
<img src="images/language/languageNO.png"></a>

<a href="index.php"><?php $language = "no"; ?>
<img src="images/language/languageEN.png"></a>

什么是更改网站的语言,并把它坚持的最好方式,当用户返回?

What is the best way to change the language of the site and have it persist when the user returns?

推荐答案

您可以通过

<a href="index.php?language=en">
<a href="index.php?language=no">

和获得语言并将其存储在cookie,并包含文件根据像

and get the languages and store them in cookie and include file according to cookie like

if ( !empty($_GET['language']) ) {
    $_COOKIE['language'] = $_GET['language'] === 'en' ? 'en' : 'nl';
} else {
    $_COOKIE['language'] = 'nl';
}
setcookie('language', $_COOKIE['language']);

和比

if ( $_COOKIE['language'] == "en") {
   include("headerEn.php");
} else {
   include("header.php");
} ?>

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

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