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: http://www.janosgyerik.com/projects/recipe-notes/

Google Play page: https://play.google.com/store/apps/details?id=com.recipenotes

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 settings.py. 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 settings.py file the CommonMiddleware comes before SessionMiddleware. Make sure their order is correct, and insert LocaleMiddleware somewhere in between, like this:

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 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:

https://docs.djangoproject.com/en/dev/topics/i18n/translation/#how-django-discovers-language-preference

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 http://jaxb.java.net/guide/Unmarshalling_is_not_working__Help_.html

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).