views:

67

answers:

2

I have a Django web site which I want ot be viewable in different languages. Until this morning everything was working fine. Here is the deal. I go to my say About Us page and it is in English. Below it there is the change language button and when I press it everything "magically" translates to Bulgarian just the way I want it. On the other hand I have a JS menu from which the user is able to browse through the products. I click on 'T-Shirt' then a sub-menu opens bellow the previously pressed containing different categories - Men, Women, Children. The link guides me to a page where the exact clothes I have requested are listed. BUT... When I try to change the language THEN, nothing happens. I go to the Abouts Page, change the language from there, return to the clothes catalog and the language is changed...

I will no paste some code.

This is my change button code:

function changeLanguage() {
    if (getCookie('language') == 'EN') {
        setCookie("language", 'BG');
    } else {
        setCookie("language", 'EN');
    }
    window.location.reload();
}

My About Us page:

@base
def aboutUs(request):
    return """<b>%s</b>""" % getTranslation("About Us Text", request.COOKIES['language'])

The @base method:

def base(myfunc):
    def inner_func(*args, **kwargs):
        try:
            args[0].COOKIES['language'] 
        except:
            args[0].COOKIES['language'] = 'BG'

        # raise Exception(request)

        # if I am in the AboutUs page 
        # and I click on the language change button
        # the cookie value in the request object changes
        # if however I am in the displayClothes page
        # the value stays the same

        # some code I removed

        contents = myfunc(*args, **kwargs)

        return render_to_response('index.html', {'title': title, 'categoriesByCollection': categoriesByCollection.iteritems(), 'keys': enumerate(keys), 'values': enumerate(values), 'contents': contents, 'btnHome':getTranslation("Home Button", args[0].COOKIES['language']), 'btnProducts':getTranslation("Products Button", args[0].COOKIES['language']), 'btnOrders':getTranslation("Orders Button", args[0].COOKIES['language']), 'btnAboutUs':getTranslation("About Us Button", args[0].COOKIES['language']), 'btnContacts':getTranslation("Contact Us Button", args[0].COOKIES['language']), 'btnChangeLanguage':getTranslation("Button Change Language", args[0].COOKIES['language'])})
    return inner_func

And the catalog page:

@base
def displayClothes(request, category, collection, page):
    clothesToDisplay = getClothesFromCollectionAndCategory(request, category, collection)

    contents = ""

    # some code I removed        

    return """%s""" % (contents)

Let me explain that you needn't be alarmed by the large quantities of code I have posted. You don't have to understand it or even look at all of it. I've published it just in case because I really can't understand the origins of the bug.

Now this is how I have narrowed the problem. I am debuging with "raise Exception(request)" every time I want to know what's inside my request object. When I place this in my aboutUs method, the language cookie value changes every time I press the language button. But NOT when I am in the displayClothes method. There the language stays the same. Also I tried putting the exception line in the beginning of the @base method. It turns out the situation there is exactly the same. When I am in my About Us page and click on the button, the language in my request object changes, but when I press the button while in the catalog page it remains unchanged.

That is all I could find, and I have no idea as to how Django distinguishes my pages and in what way.

P.S. The JavaScript I think works perfectly, I have tested it in multiple ways.

Thank you, I hope some of you will read this enormous post, and don't hesitate to ask for more code excerpts.

+1  A: 

That's far too much code to read through. You really need to make an effort to trim it down.

If you're sure that the error is somewhere in displayClothes, then I would comment bits out until you no longer get the error. But there doesn't appear to be anything there which changes the cookies in that view, so I don't know how successful you'll be. Also make sure that you're checking against what's actually in the request for AboutUs, not just what you think it is.

A side note: you're hard coding HTML directly into your views. I'm pretty sure that you don't really want to do that - Django has templates for a reason.

Anthony Briggs
A: 

OK, the issue got fixed by itself. I just deployed my application, and it works on the server, but not on my machine. Regardless, I thank all of you for your responses :)

Boris Rusev