For the internationalization of my django project, I'm using django's i18n, and I love it.
For setting the language i开发者_StackOverflow社区n the template, instead of using forms like in this example :
<form action="{{site_url}}i18n/setlang/" method="post">
<input name="next" type="hidden" value="" />
<select name="language">
{% for language in languages %}
<option value="{{language.0}}">{{language.1}}</option>
{% endfor %}
</select>
<input type="submit" value="Ok" />
</form>
I would like to use simple plain text links; something like this:
{% for language in languages %}
{% ifnotequal language.0 lang %}
<a href="{{site_url}}i18n/setlang/" >{{language.1}}</a>{% else %}{{language.1}}
{% endifnotequal %}
...
{% endfor %}
For letting the previous template snippet do his work, I've created the following jQuery function:
var languageLink = $('#language-choser > a');
languageLink.click(function(e){
var languageURL = languageLink.attr('href');
var languageNow = languageLink.text();
var lang = (languageNow=='English') ? 'en' : 'es';
$.post(languageURL, {next: "", language:lang});
});
This function works with Firefox but not with Chrome: it will simply reload the page, without changing the language.
Someone can tell me what's wrong? I've been playing around with it for a long time, without finding a way out.
EDIT Looks that it could be a caching problem. In my click function, I should clean the cached page. But how? Or should I disable browser caching for the whole site? I don't think so...
Try adding a debug statement (console.log()
, or even a simple alert()
will do) at the beginning of the handler function to make sure that it is being invoked at all. Are you adding the click handler in a document.ready()
handler?
Also I think the $.post
function will do an AJAX POST, but will not refresh the page, so you may not see anything happening at all. Try using Firebug / Chrome developer tools to examine outgoing requests to make sure.
You may want to make the click()
handler explicitly return a value too (true means continue processing the click, false means stop). Maybe that's the root of the problem: in Firefox the handler sometimes returns true
, so the link is followed, while on Chrome it returns false
and the POST is executed, but the link is not followed.
精彩评论