错误,当我在具有不同文件夹或目录的URL中时,语言未更改 [英] Error the language is not changed when I am in a url with different folders or directories
问题描述
以下参数为我的网站添加了一种语言:
The following parameter adds a language to my website:
if(isset($_COOKIE['language'])){
if($_COOKIE['language']==='en'){
$WEBSITE_TITLE = " | SITE EN";
$LANG = 'en';
$language = "en";
}elseif($_COOKIE['language']==='es'){
$WEBSITE_TITLE = " | SITE ES";
$LANG = 'es';
$language = "es";
}
} else {
$WEBSITE_TITLE = " | SITE EN";
$LANG = 'en';
$language = "en";
}
通过Ajax,我可以用西班牙语或英语更改网站的语言:
And through Ajax I can change the language of my website either in Spanish or English:
$(function() {
$(".lang").click(function(e) {
e.preventDefault();
var language = $(this).attr('data-lang');
var postData={lang: language};
var request = $.ajax({
method : 'POST',
url : 'language.ini.php',
data : postData,
dataType: "html"
});
request.done(function(data) {
//$("html").html(data);
//location.reload();
//location.href = '/index.php';
setTimeout(function(){
location.reload();
},100);
});
request.fail(function(jqXHR, textStatus) {
alert("Error!: " + textStatus);
});
});
});
通过以下参数接收Ajax发送的数据:
The data sent by Ajax is received through the following parameters:
if (isset($_POST['lang'])) {
$lang = $_POST['lang'] ?: '';
if ($lang === "en") {
setcookie ('language', 'en', time()+60*60*24*365, '/', 'example.com');
} elseif ($lang === "es") {
setcookie ('language', 'es', time()+60*60*24*365, '/', 'example.com');
}
}
但是错误是当我尝试更改具有多个文件夹或目录的url中的语言时
But the error is when I try to change the language in a url with several folders or directories:
example.com/es/folders2/folders3/folders4/url-of-article/
当前,更改语言的代码仅对以下页面适用:
Currently the code to change the language only works for me on these pages:
example.com
example.com/index.php
推荐答案
将 dataType:"html"
更改为 dataType:"json"
并添加console.error(jqXHR);
进入失败功能转到控制台,看看得到了什么.
Change dataType: "html"
to dataType: "json"
and add console.error(jqXHR);
in to your fail function goto console see what you get.
您可能对数据类型有疑问.
You probably having problem with data type.
我对此进行了测试,对我来说效果很好:
I tested this and it works fine for me :
$(function() {
$(".lang").click(function(e) {
e.preventDefault();
var language = $(this).attr('data-lang');
var postData={lang: language};
var request = $.ajax({
method : 'POST',
url : 'language.ini.php',
data : language,
dataType: "JSON"
});
request.done(function(data) {
//$("html").html(data);
//location.reload();
//location.href = '/index.php';
$("#results").html(data); //show data
setTimeout(function(){
location.reload();
},100);
});
request.fail(function(jqXHR, textStatus) {
console.error(jqXHR);
alert("Error!: " + textStatus);
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="lang"> click me </div>
<div id="result"> </div> // Conent will show in this div.
这篇关于错误,当我在具有不同文件夹或目录的URL中时,语言未更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!