Monthly Archives: September 2012

Recipe Notes

I made something. A simple Android app. If you like cooking and collecting recipes of delicious dishes that you actually eat, this app is for you. Entering recipes is usually a lot of work. This app aims to minimize that effort, especially typing, so that you can note down all the delicious dishes you eat with photos as easily as possible.

Project homepage:

Google Play page:

Android app on Google Play

Detect language preference in Django and confirm it works

Django has built-in facility for detecting the preferred language of clients, but it is not enabled by default.

First of all, you need to enable the django.middleware.locale.LocaleMiddleware in the MIDDLEWARE_CLASSES configuration in The ordering of the middleware classes is relevant, in particular LocaleMiddleware must come after SessionMiddleware and before CommonMiddleware. A bit of a “gotcha” there is that in the default file the CommonMiddleware comes before SessionMiddleware. Make sure their order is correct, and insert LocaleMiddleware somewhere in between, like this:

    # other middleware ...

As a result, Django will fill in the detected language preference of the client in all request objects as request.LANGUAGE_CODE, in a format like en for English, pt-br for Brazilian Portugese, and so on.

To test that it works, create a primitive view method like this (and wire it to a url handler):

from django.http import HttpResponse

def mylang(request):
    return HttpResponse("request.LANGUAGE_CODE = %s\n" % request.LANGUAGE_CODE)

It is easiest to fake a client preference and thereby test the preference is correctly detected using curl or wget:

curl http://localhost:8000/mylang
curl http://localhost:8000/mylang --header 'Accept-Language: de'
curl http://localhost:8000/mylang --header 'Accept-Language: de' --cookie django_language=hu

For more details see the official documentation:

Debugging strange unmarshalling problems when using jaxb

The other day I had a very strange unmarshalling issue with jaxb: unmarshalling (deserializing) from an XML file and then marshalling (serializing) the same object to another XML file resulted in different content. There were no errors thrown in the process and I had no idea as to why.

Finally I found a way to debug it on this page

Unmarshaller u = ...;
// this implementation is a part of the API and convenient for trouble-shooting,
// as it prints out errors to System.out
u.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());

u.unmarshal(new File("foo.xml"));

Based on the errors reported by the event handler it was straightforward to fix the problem (some property files that relax some validation rules were not on the classpath).