Skip to main content

Internationalization with flags of main languages, It lets easy to integrate the Django i18n in yours templates

Project description

Django Translation Flags

Build Status Maintainability codecov PyPI - Downloads PyPI - Version

This Django app provides integration for translation options in templates with some most common standard world languages. This is useful fow when you need to display language options in yours Django Apps.


Django Translation Flags require Django Internationalization and localization properly configured. You can see more about these settings in

Basically you need to:

  1. Define a custom LANGUAGES list on with tuples, i.e:

from django.utils.translation import gettext_lazy as _

  ('de', _('German')),
  ('en', _('English')),
  ('pt-br', _('Brazilian Portuguese'))

Only languages listed in the LANGUAGES setting can be selected. This example restricts languages that are available for automatic selection to German, English and Brazilian Portuguese

  1. Add Middleware

To use LocaleMiddleware, add ‘django.middleware.locale.LocaleMiddleware’ to your MIDDLEWARE setting. Because middleware order matters, follow these guidelines:

  • Make sure it’s one of the first middleware installed.

  • It should come after SessionMiddleware, because LocaleMiddleware makes use of session data. And it should come before CommonMiddleware because CommonMiddleware needs an activated language in order to resolve the requested URL. If you use CacheMiddleware, put LocaleMiddleware after it.

For example, your MIDDLEWARE might look like this:

  1. Markup the text to translation:

The format of .po files is straightforward. Each .po file contains a small bit of metadata, such as the translation maintainer’s contact information, but the bulk of the file is a list of messages – simple mappings between translation strings and the actual translated text for the particular language.

For instance, if your Django app contained a translation string for the text “Welcome to my site.”, like so:

from django.utils.translation import gettext_lazy as _
_("Welcome to my site.")

…then django-admin makemessages will have created a .po file containing the following snippet – a message:

#: path/to/python/
msgid "Welcome to my site."
msgstr ""
  1. Generate and compile it using the commands bellow:

  • The first step is to create a message file for a new language:

django-admin makemessages -l de -l en -l pt_BR
  • Compiling message files after creating your message file:

django-admin compilemessages

For more detailed information on how to create language files it is suggested to read the documentation:


Install from PyPI:

pip install django-translation-flags


Add django-translation-flags to your list of INSTALLED_APPS in


Add the Django Translation Flags URLs to

from django.conf.urls import url, include

urlpatterns = [
    path('i18n/', include('django_translation_flags.urls')),

Inject the required meta tags in your base.html (or wherever your HTML <head> is defined):

{% load flags %}

    {% languages %}

By default it will show the rectangular icons, but you can change it to square:

{% load flags %}

    {% languages 'square' %}

Optionally you can set your custom class for HTML tags:

{% load flags %}

    {% languages 'square' li_class='your-li-class' a_class='your-a-class' %}

The languages template tags accept **kwargs to configure the class to HTML tags. So you can set the classes to these HTML tags:

li_class: Class to li tag (Default: empty)

a_class: Class to a tag (Default: empty)

The HTML structure is:


How does it work?

The Django Translation Flags has a CSS file where all the most important languages flags are configured.

The avaliable flags are:

af: Afrikaans, ar: Arabic, az: Azerbaijani, de: German, en: English, en-au: Australian English, es: Spanish, es-ar: Argentinian Spanish, es-mx: Mexican Spanish, fr: French, hi: Hindi, hu: Hungarian, id: Indonesian, it: Italian, ja: Japanese, ko: Korean, nl: Dutch (Nederlands), pl: Polish, pt: Portuguese, pt-br: Brazilian Portuguese, ru: Russian, sv: Swedish, tr: Turkish, uk: Ukrainian, zh-cn: Simplified Chinese, zh-hans: Simplified Chinese and zh-hant: Traditional Chinese.


The App get the language code from LANGUAGES on and then it concatenates the language codes with the name of the icon class and shows the correct flags..

See the all Django supported languages in module django.conf.locale.LANG_INFO LANG_INFO is a dictionary structure to provide meta information about languages.


Feedback and pull requests are strongly encouraged and kindly appreciated (-:



  1. Clone the repository.

  2. Create a virtualenv with Python 3.6 or 3.7

  3. Active the virtualenv.

  4. Install the dependencies.

  5. Run the tests.

git clone
cd django-translation-flags
python -m venv .venv
pip install -r requirements.txt

Less to CSS

You will need of node and npm previously installed.

  1. Install the dependencies

  2. Run the gulp

  3. Edit the less files: /assets/less/

npm install
npm run build


All files in this repository are distributed under the MIT license.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-translation-flags-1.0.6.tar.gz (141.3 kB view hashes)

Uploaded source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page