2014-01-07 49 views
2

我正在尝试创建一个网站,在第一次访问时显示所选语言页面。单击语言名称时,语言名称字符串(例如法语)将存储在Web /本地存储中。当用户下次访问网站时,将检查语言字符串并显示特定的语言页面。 我写了12个函数来存储每种语言的锚点标签点击时的语言字符串。像这样:用于网站本地化的网络存储

function english() 
{ 
if(typeof(Storage)!=="undefined") 
    { 
    localStorage.clear(); 
    localStorage.language="English"; 
window.location.reload(); 
    } 
} 

function french() 
{ 
if(typeof(Storage)!=="undefined") 
    { 
    localStorage.clear(); 
    localStorage.language="French"; 
    window.location.href = "french.html"; 
    } 
} 

这种方法真的有效吗?我认为为多种语言创建多个页面对搜索引擎优化很有用,而不是随时更改文本字符串。 直到我在查看Chrome开发者控制台时,字符串的值才被保存。但是,当我关闭标签页或浏览器窗口时,存储的值将消失,下次它不记得语言时。

我使用这个代码检查语言(我必须做的12种语言):

window.onload = function() { 
if (localStorage.language="Language") 
    { 
    window.location.href = "language.html"; 
    } 
else if (localStorage.language="English") 
    { 
    window.location.href = "eng.html"; 
    } 
else if (localStorage.language="French") 
    { 
    window.location.href = "french.html"; 
    } 
else if (localStorage.language="Spanish") 
    { 
    window.location.href = "spanish.html"; 
    } 

的代码检查后还挺停止第一“如果”的条件,真的还是假的,它只是停在那里除此之外,不检查'else if'条件。

有人可以帮助我吗?即使在浏览器关闭后,示例代码似乎仍将值存储在Web存储中,但我无法将其复制到我的浏览器中。

回答

8

您遇到的问题是使用=而不是===来比较(正如wonko79所解释的那样)。至于如何改进你所做的代码 - 这里有很多问题,最重要的是很多重复的信息是什么检查和应用什么,这是一个危险,因为它使维护困难。我放在一起解决您的问题用一个对象来存储所有关于语言的信息,并创建接口挑选来自同一数据集的语言:

var locales = { 
    'us': {label: 'English',location:'english.html'}, 
    'de': {label: 'German',location: 'german.html'}, 
    'fr': {label: 'Français',location: 'french.html'}, 
    'nl': {label: 'Nederlands',location: 'dutch.html'}  
}; 

http://jsfiddle.net/codepo8/KzNUM/

我也写了一个较长的博客文章更多的细节http://christianheilmann.com/2014/01/08/this-developer-wanted-to-create-a-language-selector-and-asked-for-help-on-twitter-you-wont-believe-what-happened-next/

+0

难道你不是指'德意志'? – Neil

0

据我可以看到你做assignements而不是比较。

下面的代码应该工作:

window.onload = function() { 
    if (localStorage.language=="Language") 
     { 
     window.location.href = "language.html"; 
     } 
    else if (localStorage.language=="English") 
     { 
     window.location.href = "eng.html"; 
     } 
    else if (localStorage.language=="French") 
     { 
     window.location.href = "french.html"; 
     } 
    else if (localStorage.language=="Spanish") 
     { 
     window.location.href = "spanish.html"; 
     } 

更多信息请参见JavaScript Comparison and Logical Operators

1

正如wonko79已经指出的,问题是,你的if检查里面你居然重新分配的变量,而不是检查其内容。要真正检查字符串内容是否相等,您需要使用两个等号==(或者如果您想检查类型是否相等,则为三个)。此外,每当你不断重复自己的想法,这是一个很好的迹象,你可以改善你的代码。在这种情况下,您总是检查localStorage.language是否等于某种语言,如果是这种情况,那么您将位置更改为该语言的URL。因此,本质上,您是查找存储在本地存储中的语言的URL。

JavaScript具有高效的查找数据结构:对象。因此,要改善你的情况,我们可以首先将数据存储在一个对象:

var languageUrls = { 
    'language': 'language.html', 
    'english': 'eng.html', 
    'french': 'french.html', 
    'spanish': 'spanish.html' 
}; 

后,我们已经做了,我们可以简单地查找立即基础上,localStorage.language值的值:

window.onload = function() { 
    // we can perform the lookup by providing the *key* in brackets: 
    var url = languageUrls[localStorage.language]; 

    // in case the key did not exist in our object, url will be null 
    if (!url) { 
     // so we might want to provide a fallback value here 
     url = 'eng.html'; 
    } 

    // now we have a valid URL in our `url` variable 
    window.location.href = url; 
} 

这就是你需要做的一切;不需要重复检查if语句,只需简单查找即可。而当你想添加另一种语言时,只需要向languageUrls对象添加另一个条目。

一个类似的想法可以应用于你的语言功能,它改变本地存储中存储的语言。相反,有多个不同的功能,它的足有一个函数,它接受的语言作为一个参数,然后再次使用查找更改URL:

function changeLanguage (language) { 
    // it’s always a good idea to normalize input; 
    // in this case, keep everything lower-case 
    language = language.toLowerCase(); 

    // this time, we might want to make sure first that the language is 
    // valid – all valid languages are stored in our `languageUrls` object, 
    // so we can use that 
    var url = languageUrl[language]; 

    if (!url) { 
     // There was no entry for the language—it’s not a valid language—so 
     // we just return from the function here, not doing anything. We could 
     // show an error though. 
     return; 
    } 

    // so the language is valid, and the target URL is stored in `url` 

    if (typeof(Storage) !== 'undefined') { 
     // update the stored language; note that I’m not calling `clear` 
     // because we might want to store other information in the future 
     // too. It’s enough to just overwrite the stored language. 
     localStorage.language = language; 
    } 

    // we still want to redirect, even if the storage was not available 
    window.location.href = url; 
} 

这就是你需要设置已经一切。剩下的就是将电话从english()更改为changeLanguage('english')