记住首选语言 [英] Remember preferable language

查看:148
本文介绍了记住首选语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的女儿做了一个简单的网站。
荷兰语,每一页都有英语版本。

I've made a simple website for my daughter. It is in Dutch and for every page there is a English version as well.

荷兰语URL:nl / index.html

Dutch URL: nl/index.html

英语网址:eng / index.html

English URL: eng/index.html

我想要做的是给访问者选择一种语言作为偏好。所以如果他们来到这个网站,下一次他们会自动链接到首选页面。
我知道这可以做一个cookie,看到在这个论坛上的解释(如何记住当前点击的URL?javascript?PHP?)。

What I would like to do is give the visitor the option to set one language as preference. So if they come to this site the next time they will automatically linked to the preferable page. I know this can be done with a cookie and saw the explanation on this forum ( How to remember the currently clicked url? javascript?PHP? ).

我试图使这项工作,但显然我做错了什么?
有人可以指导我一步一步吗?这将是巨大的!

I've tried to make this work but apparently I am doing something wrong? Can somebody guide me through step by step? That would be great!

亲爱的,
Jurgen

Kind regards, Jurgen

推荐答案

如果您熟悉jQuery,则可以使用 Cookie插件来保留用户的语言选择,并在他每次返回您的网站时将他重定向到相应的页面。 Bellow是一个使用两个按钮设置语言的示例代码:

If you are familiar with jQuery you can use the cookies plug-in to persist the user's language choice and redirect him to the appropriate page every time he comes back to your site. Bellow is a sample code that uses two buttons to set the language:

首先声明jQuery脚本(我用来将它们存储在脚本文件夹,因此下面的):

First you declare the jQuery scripts (I use to store them in a Script folder, hence the following):

<script type="text/javascript" src="../Script/jquery-1.7.2.js"></script>
<script type="text/javascript" src="../Script/jquery.cookie.js"></script>

然后定义页面就绪事件,如下所示:

Then you define the page ready event like this:

$(function () {

    var url = 'your_url';
    var english_page = 'eng/index.html';
    var dutch_page = 'nl/index.html';

    if ($.cookie('default_page') != null) {
        if (window.location.href != url + '/' + $.cookie('default_page')) {
            window.location.href = url + '/' + $.cookie('default_page');
        }
    }

    $('#set_english_butt').click(function () {
        $.cookie('default_page', english_page, { expires: 999 });
        alert('English was set as the default language');
    });

    $('#set_dutch_butt').click(function () {
        $.cookie('default_page', dutch_page, { expires: 999 });
        alert('Dutch was set as the default language');
    });

});

在您的页面中挂钩了一些HTML按钮:

Which is hooked to some html buttons in you page:

<div>
    <span>Select your language:</span>
    <button id="set_english_butt">English</button>
    <button id="set_dutch_butt">Dutch</button>
</div>

这篇关于记住首选语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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