Hi everybody,
we just finished developing a major multi-language site for a customer. In the course of this development, we run into two issues with the PO files which I haven't been able to find either on the mailing list or in the bug tracker so I'll raise them here:
We're using country-specific translations as well as a fallback translation, for instance "de-de" (Germany) and "de-ch" (Switzerland) as well as the global "de" (German) translation. In case the country-specific translation doesn't exist or does not contain a certain string, it should fall back to the global version. However, while django.utils.translation.trans_real talks about fallbacks (for instance here:
https://github.com/django/django/blob/master/django/utils/translation/trans_real.py#L142), the actual fallback does not happen. If the country-specific locale does not exist, the global version is not being used either.
We solved it with this middleware:
from django.middleware.locale import LocaleMiddleware
from django.utils.translation.trans_real import _translations, translation
class FallbackLocaleMiddleware(LocaleMiddleware):
def process_request(self, request):
super(FallbackLocaleMiddleware, self).process_request(request)
if '-' in request.LANGUAGE_CODE:
trans = _translations[request.LANGUAGE_CODE]
if not trans._fallback:
trans.add_fallback(translation(request.LANGUAGE_CODE.split('-', 1)[0]))
Shouldn't this rather be something along the lines of:
if '-' in language:
default_translation = _fetch(language.split('-')[0]) or _fetch(settings.LANGUAGE_CODE)
else:
default_translation = _fetch(settings.LANGUAGE_CODE)
I might be misunderstanding the Django translation logic so I wanted to raise this issue here before filing a ticket. Feedback is very welcome.
Cheers,
Jonas