[Django]-Internationalized Django app back to English

5👍

You need to do multiple things:

  • Write all strings in one language, in your case English.

You then need to mark all strings for translation:

  • Add trans tags arounds all strings in templates (from the i18n template tag library), see internationalization in the documentation. This is related to gettext: a common library for handling translation. By adding trans tags around strings they will be included in a huge list of strings that need to be translated (see .po and .mo below).
  • Add _() around all strings in the Python source code (from django.utils.translation, also see internationalization in the docs). This is also related to gettext and this includes those strings as well in the gettext language files.

You then need to create the language files and add the translations to those files:

  • Create the gettext .po and .mo files which contain the translation strings. In these files you can write the translated strings. These two files are often placed in the /locale/ directory. For each language you need to write the translated strings and put them in these files. More details on these language files can be found in the Django documentation on localization.

It’s good to read Django’s overview on internationalization and localization (often called i18n) as the above explanation is only the general way to do it. The exact technical details are in the documentation.

Note that the above is only for translating static strings that are part of your Python code or templates. If you want to translate URLs or Django models then you’ll need to install additional libraries or use other existing Django functionality. For example, in Django 1.4 some functionality was added to translate URLs but for earlier Django versions you’ll need to use a library / Django app for that.

0👍

Simeon’s answer covers all ground but here, I have found something that might help anyone test the localization in development environment.

If you are native European then your browser may request the accepted-language as es, just for the example.

Now, you want to test the English language translation in your local environment then you need to consider below possibilities.

As per django-book‘s refrence:

You will need to put LocaleMiddleware

1> First, it looks for a django_language key in the current user’s
session.

2> Failing that, it looks for a cookie.

3> Failing that, it looks at the Accept-Language HTTP header. This
header is sent by your browser and tells the server which language(s)
you prefer, in order by priority. Django tries each language in the
header until it finds one with available translations.

4> Failing that, it uses the global LANGUAGE_CODE setting.

Now, you see it will check LANGUAGE_CODE setting as the last resort.

So, you can use HTTP-request interceptor in browser and change the accepted-language value to test your language translation on the fly.

Leave a comment