I am trying to build a web site in both English and Bulgarian using the Django framework. My idea is the user should click on a button, the page will reload and the language will be changed. This is how I am trying to do it:
In my html I hava a the button tag <button id='btn' onclick="changeLanguage();" type="button"> ... </button>
An excerpt from cookies.js:
function changeLanguage() {
if (getCookie('language') == 'EN') {
document.getElementById('btn').innerHTML = getCookie('language');
setCookie("language", 'BG');
} else {
document.getElementById('btn').innerHTML = getCookie('language');
setCookie("language", 'EN');
}
}
function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure) {
var sCookie = sName + "=" + encodeURIComponent(sValue);
if (oExpires) {
sCookie += "; expires=" + oExpires.toGMTString();
}
if (sPath) {
sCookie += "; path=" + sPath;
}
if (sDomain) {
sCookie += "; domain=" + sDomain;
}
if (bSecure) {
sCookie += "; secure";
}
document.cookie = sCookie;
}
And in my views.py file this is the situation
@base
def index(request):
if request.session['language'] == 'EN':
return """<b>%s</b>""" % "Home"
else request.session['language'] == 'BG':
return """<b>%s</b>""" % "??????"
So I know that my JS changes the value of the language cookie but I think Django doesn't get that. On the other hand when I set and get the cookie in my Python code again the cookie is set. My question is whether there is a way to make JS and Django work together - JavaScript sets the cookie value and Python only reads it when asked and takes adequate actions?
Thank you.