Tag Archives: multilingual

Django multilingual: select translation instead of ‘None’

If you use the django-multilingual module for multilingual content, you need to add a translation for every language your entity could be seen through. Otherwise you get 'None' as a title (which is way too blurry and quite indescribable). Thanks to my colleague and friend lpetrov I found a way to pass through this limitation and print any possible translation if the current one is not available.

I've added a translation.py file in my project which I import in all my apps when necessary. Here there is the code:

  1. def get_local_str_or_other(obj, field_name):
  2. result = getattr(obj, field_name)
  3. if result:
  4. return result
  5. else:
  6. field_for_search = field_name + "_"
  7. translations = [getattr(obj, field_for_search + lang[0].replace("-","_")) for lang in settings.LANGUAGES]
  8. for translation in translations:
  9. if translation != None:
  10. return unicode(translation)

Later I import the function in my models and I redefine the __unicode__ function. If the name I want to print by default is the field 'title', we have the following:

  1. def __unicode__(self):
  2. return get_local_str_or_other(self, "title")


del.icio.us Digg DZone Facebook Google Google Reader Magnolia reddit SlashDot Technorati ReadMe.ru Dobavi.com Dao.bg Lubimi.com Ping.bg Pipe.bg Svejo.net Web-bg.com

Django translations: multilingual, i18n, app names

 Django is a popular web framework written in Python that provides dynamic generated content and allows rapid development for different systems in the Internet. It provides a great interface to other products (via web services), gives ability to design a scalable application and most important, lies on a powerful language as Python (in other words, what you could do in Python, you could somehow reuse it in Django either by a component or by defining a custom logic down there).

In my experience two of the most important things in a web application (no matter the framework, system and language) are the user management and multilingual support. Django has an Auth module that does most of the things you need - provide users and groups, powerful permission API for each model and action down there and many methods to be overriden from the framework to fine tune the filtering if needed. As for the multilingual support, there are few steps to be followed.

Translating the static content

i18n support for Django - in the Django book. You have the uggettext_lazy (and two more) functions to translate the local based strings in your models, views and so on logic python parts. As for the templates, you could use trans and blocktrans tags for the static content down there.

Translating files is easy - via 'makemessages' and 'compilemessages' you get .po files (for PoEdit or any text editor) and then you compile them for Django to interpret.

Translating models and app names

Translating models for the admin is easy - just add the verbose_name and verbose_name_plural in your Meta class and make that visible for the makemessages.

App names is kind of a mystery for me as well. I saw this guy using the __init__.py and adding application names there. It works for me - my application recognizes them and generates the appropriate literals in the .po files.  


A module named django-localeurl could address all your request to a certain subdirectory for a given language - lets say site.com/en , site.com/es and so on. Based on that your content could automatically detect the current language and load the right translation. Nice, huh?

Multilingual content

django multilingual exampleWhat if you need a content translation for every language in your site? Either it is a news portal or a blog, you could add a new language in the settings.py anytime and just get an additional text field in the admin panel for the new language. This is why django-multilingual exists. You define the fields to be multilingual and you get a field for every language. With the help of localeurl you just get the right translation in the end with some automagic in between :)


At the moment I do use a CountryMiddleware to get the country on request via subdomains. I have Country model which has a name (of the country) and subdomain (for the subdomain). 

A trick to retrieve the subdomain of a webpage you could see here. Then all you need is to call your model and use the get_object_or_404 function to add this one to your request (so the country is visible globally).


del.icio.us Digg DZone Facebook Google Google Reader Magnolia reddit SlashDot Technorati ReadMe.ru Dobavi.com Dao.bg Lubimi.com Ping.bg Pipe.bg Svejo.net Web-bg.com