mozilla/olympia — pull request #18 by magopian (2014-03-21 13:01:28+00:00)
Title: Locale priorization for translations (bug 983749)

The aim of this PR is to have various fallbacks when requesting a translation,
and not only one as we have at the moment.

Before: first get the translation for the active language, and if there's none,
check if there's a fallback translation (either in a locale provided via
```model.get_fallback()```, or the ```settings.LANGUAGE_CODE```).

After: Check the following locales in order
1/ the active language
2/ the model fallback provided by the ```get_fallback()``` method (if it exists)
3/ ```settings.LANGUAGE_CODE```
4/ in last resort return just any translation found

This PR is quite hairy, as I implemented it in a way to minimize the number of
joins: if the three locales to choose from are all the same, just join twice
(once for the locale, and once for the "last chance" fallback returning any
translation) instead of four times.

I've tried to factorize as much code possible between ```translation.query```
and ```translation.transformer.build_query```, but both are using different
APIs, and for different use cases, so the only bit in common is the
```get_locales``` function.

I've tried to comment it as thoroughly as possible to ease the review and the
maintenance, but I aknowledge that it's still a big piece of SQL generation,
and deep django code.
If you need help understand some pieces of the code, please let me know and
I'll try my best to explain it.