我编写了检查用户语言并打开文件的代码(如果用户的语言为英语,则打开en.php,依此类推)。我添加了一个选项,可以在设置中更改您的语言,但这不起作用。
# Checks for user's language
if (isset($_COOKIE['lang'])) {
$lang = substr(explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE'])[0],0,2)?:'en';
if ($lang = $_COOKIE['lang']){
$lang = $_COOKIE['lang'];
setcookie("lang",$lang,time()+3650*24*60*60);
$_SESSION['lang'] = $lang;
} else {
# $lang = substr(explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE'])[0],0,2)?:'en';
setcookie("lang",$lang,time()+3650*24*60*60);
$_SESSION['lang'] = $lang;
}
}
if ($lang === "en") {
require 'translations/en.php'; # ENGLISH
} # also every other language is included in elseif
<!-- The form goes to that same page but at the beginning there are files included -->
<form method="post" action="settings.php" id="form2" name="form2">
<select class="settingsselect" name="language_select">
<option disabled selected>Select a Language</option>
<option value="something">some language</option>
</select>
</form>
<button class="updatesettings" type="submit" id="updatesettings" name="updatesettings" form="form2">Update Settings</button>
# Update code
if (isset($_POST['updatesettings'])) {
$lang = $_POST['language_select'];
setcookie("lang",$lang,time()+3650*24*60*60);
$_SESSION['lang'] = $lang;
}
#files included in the beginning
require 'language_change.php';
require 'password_check.php';
require 'translation.php';
答案 0 :(得分:0)
您提供的php
文件的一些示例是近似的且充满语法错误,由于您的方法不正确,可能无法正常工作,请尝试更改php
的逻辑...并由于我们无法猜测出什么问题(您的code
应该从头开始重写),因此我创建了一个php
示例,说明如何工作,请不要复制它,但要从中获取启发,谢谢
index.php
<?php
@ini_set('default_charset', 'UTF-8');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
<!DOCTYPE html>
<html lang="<?php echo isset($_COOKIE['language']) ? htmlspecialchars($_COOKIE['language'], ENT_QUOTES, 'UTF-8') : (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2) : 'en'); ?>">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex, nofollow">
<title>Change Language</title>
<?php
function check_https() {
$isSecure = false;
if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
$isSecure = true;
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && strtolower($_SERVER['HTTP_X_FORWARDED_SSL']) == 'on') {
$isSecure = true;
}
return $isSecure;
}
$check_https = check_https() ? 'https' : 'http';
if (isset($_POST['change'])) {
switch ($_POST['language']) {
case 'en':
if ($check_https == 'http') {
setcookie('language', 'en', time()+31556926 , '/', NULL, NULL, TRUE);
} else {
setcookie('language', 'en', time()+31556926 , '/', NULL, TRUE, TRUE);
}
$_COOKIE['language'] = 'en';
break;
case 'it':
if ($check_https == 'http') {
setcookie('language', 'it', time()+31556926 , '/', NULL, NULL, TRUE);
} else {
setcookie('language', 'it', time()+31556926 , '/', NULL, TRUE, TRUE);
}
$_COOKIE['language'] = 'it';
break;
case 'fr':
if ($check_https == 'http') {
setcookie('language', 'fr', time()+31556926 , '/', NULL, NULL, TRUE);
} else {
setcookie('language', 'fr', time()+31556926 , '/', NULL, TRUE, TRUE);
}
$_COOKIE['language'] = 'fr';
break;
case 'es':
if ($check_https == 'http') {
setcookie('language', 'es', time()+31556926 , '/', NULL, NULL, TRUE);
} else {
setcookie('language', 'es', time()+31556926 , '/', NULL, TRUE, TRUE);
}
$_COOKIE['language'] = 'es';
break;
default:
}
}
?>
<link rel="shortcut icon" href="/favicon.ico">
<style>
<!--
html {
min-width: 360px !important;
}
.lang-content {
width: 210px;
}
-->
</style>
</head>
<body>
<div class="lang-content">
<fieldset><legend>Change Language</legend>
<form name="language" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<select name="language" class="lang">
<option class="my_language" value="en"<?php if (isset($_COOKIE['language']) && $_COOKIE['language'] == 'en') { echo ' selected'; } ?>>en</option>
<option class="my_language" value="it"<?php if (isset($_COOKIE['language']) && $_COOKIE['language'] == 'it') { echo ' selected'; } ?>>it</option>
<option class="my_language" value="fr"<?php if (isset($_COOKIE['language']) && $_COOKIE['language'] == 'fr') { echo ' selected'; } ?>>fr</option>
<option class="my_language" value="es"<?php if (isset($_COOKIE['language']) && $_COOKIE['language'] == 'es') { echo ' selected'; } ?>>es</option>
</select>
<input class="button" type="submit" name="change" value="Change Language">
</form>
</fieldset>
</div>
<?php
if (isset($_COOKIE['language'])) {
$my_language = htmlspecialchars($_COOKIE['language'], ENT_QUOTES, 'UTF-8');
if (file_exists(realpath("$my_language.php"))) {
include("$my_language.php");
echo PHP_EOL;
echo PHP_EOL;
} else {
include("en.php");
echo PHP_EOL;
echo PHP_EOL;
}
} else {
$my_language = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2) : 'en';
if (file_exists(realpath("$my_language.php"))) {
include("$my_language.php");
echo PHP_EOL;
echo PHP_EOL;
} else {
include("en.php");
echo PHP_EOL;
echo PHP_EOL;
}
}
?>
</body>
</html>
en.php
<?php
echo "Welcome!";
?>
es.php
<?php
echo "Bienvenida!";
?>
fr.php
<?php
echo "Bienvenue!";
?>
it.php
<?php
echo "Benvenuto!";
?>
生成的html
如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex, nofollow">
<title>Change Language</title>
<link rel="shortcut icon" href="/favicon.ico">
<style>
<!--
html {
min-width: 360px !important;
}
.lang-content {
width: 210px;
}
-->
</style>
</head>
<body>
<div class="lang-content">
<fieldset><legend>Change Language</legend>
<form name="language" action="/test/index.php" method="post">
<select name="language" class="lang">
<option class="my_language" value="en" selected>en</option>
<option class="my_language" value="it">it</option>
<option class="my_language" value="fr">fr</option>
<option class="my_language" value="es">es</option>
</select>
<input class="button" type="submit" name="change" value="Change Language">
</form>
</fieldset>
</div>
Welcome!
</body>
</html>
我希望这会有所帮助。