我有一个支持两种语言的网站。在Chrome中,切换语言效果很好,但在Firefox中,我点击哪个按钮并不重要,对于“英语”或“德语”语言,它总是为默认设置的德语设置我的语言变量
有人可以帮我解决这个问题吗?
这是我调用getLangCookie函数的示例函数
<?php
function convert_number_to_words($number) {
$hyphen = '-';
$conjunction = ' and ';
$separator = ', ';
$negative = 'negative ';
$decimal = ' point ';
$dictionary = array(
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
10 => 'ten',
11 => 'eleven',
12 => 'twelve',
13 => 'thirteen',
14 => 'fourteen',
15 => 'fifteen',
16 => 'sixteen',
17 => 'seventeen',
18 => 'eighteen',
19 => 'nineteen',
20 => 'twenty',
30 => 'thirty',
40 => 'fourty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
100 => 'hundred',
1000 => 'thousand',
1000000 => 'million',
1000000000 => 'billion',
1000000000000 => 'trillion',
1000000000000000 => 'quadrillion',
1000000000000000000 => 'quintillion'
);
if (!is_numeric($number)) {
return false;
}
if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
// overflow
trigger_error(
'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
E_USER_WARNING
);
return false;
}
if ($number < 0) {
return $negative . convert_number_to_words(abs($number));
}
$string = $fraction = null;
if (strpos($number, '.') !== false) {
list($number, $fraction) = explode('.', $number);
}
switch (true) {
case $number < 21:
$string = $dictionary[$number];
break;
case $number < 100:
$tens = ((int) ($number / 10)) * 10;
$units = $number % 10;
$string = $dictionary[$tens];
if ($units) {
$string .= $hyphen . $dictionary[$units];
}
break;
case $number < 1000:
$hundreds = $number / 100;
$remainder = $number % 100;
$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
if ($remainder) {
$string .= $conjunction . convert_number_to_words($remainder);
}
break;
default:
$baseUnit = pow(1000, floor(log($number, 1000)));
$numBaseUnits = (int) ($number / $baseUnit);
$remainder = $number % $baseUnit;
$string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
if ($remainder) {
$string .= $remainder < 100 ? $conjunction : $separator;
$string .= convert_number_to_words($remainder);
}
break;
}
if (null !== $fraction && is_numeric($fraction)) {
$string .= $decimal;
$words = array();
foreach (str_split((string) $fraction) as $number) {
$words[] = $dictionary[$number];
}
$string .= implode(' ', $words);
}
return $string;
}
echo convert_number_to_words(100000 );
// one hundred thousand
?>
答案 0 :(得分:0)
我认为你应该把ajax请求放到onload函数上,就像这样。
function getLangCookie(lang) {
var name = lang + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// by clicking on English button set the cookie value
function onEnglishbtn() {
setLangCookie("lang", "en", 30);
document.location.reload();
var lang = getLangCookie('lang');
return lang;
}
function setLangCookie(lang, value, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = lang + "=" + value + "; " + expires;
}
// function onload from index.html setting up the lang by default
window.onload = function () {
var lang_cookie = getLangCookie('lang');
console.log('lang_cookie = ', lang_cookie);
// if cookie doesn't exist
if (lang_cookie !== null) {
setLangCookie("lang", "de", 30);
} // if cookie exists
else {
console.log('lang_cookie exists!');
setLangCookie("lang", lang_cookie, 30);
}
$.ajax({
type: "GET",
url: '/menu.xml',
dataType: "xml",
success: function (xml) {
$(xml).find('description ' + lang_cookie).each(function () {
$(this).parent().html($(this).html());
}
);
var menu = [];
var data = $.xml2json(xml)['#document'];
that.menu = data.menu;
console.log('menu = ', that.menu);
}
});
}