Skip to main content

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.

Requirements

Django Translation Flags require Django Internationalization and localization properly configured. You can see more about these settings in https://docs.djangoproject.com/en/2.1/topics/i18n/

Basically you need to:

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

from django.utils.translation import gettext_lazy as _

LANGUAGES = [
  ('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:

MIDDLEWARE = [
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.locale.LocaleMiddleware',
   'django.middleware.common.CommonMiddleware',
]
  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/module.py:23
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: https://docs.djangoproject.com/en/2.1/topics/i18n/translation/#how-to-create-language-files

Install

Install from PyPI:

pip install django-translation-flags

Configuration

Add django-translation-flags to your list of INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    ...
    'django_translation_flags',
    ...
]

Add the Django Translation Flags URLs to urls.py:

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 %}

<ul>
    {% languages %}
</ul>

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

{% load flags %}

<ul>
    {% languages 'square' %}
</ul>

Optionally you can set your custom class for HTML tags:

{% load flags %}

<ul>
    {% languages 'square' li_class='your-li-class' a_class='your-a-class' %}
</ul>

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:

<li>
    <a>
        <span></span>
    </a>
</li>

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.

image5

The App get the language code from LANGUAGES on settings.py 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

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

Contributing

Python

  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 https://github.com/silviolleite/django-translation-flags
cd django-translation-flags
python -m venv .venv
.venv/bin/activate
pip install -r requirements.txt
python runtests.py

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

Licensing

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

Metadata

Release files for django-translation-flags 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-translation-flags 1.0.6
File Size Uploaded Provenance Actions
django-translation-flags-1.0.6.tar.gz 141.3 kB Without provenance Details for django-translation-flags-1.0.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-translation-flags 1.0.6
File Interpreter ABI Platform Actions
django_translation_flags-1.0.6-py3-none-any.whl Python 3 none any Details for django_translation_flags-1.0.6-py3-none-any.whl

Total release size: 300.8 kB

Release files / django-translation-flags-1.0.6.tar.gz

Download URL django-translation-flags-1.0.6.tar.gz
Size 141.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c54973fdb4b570831c2e59bbb2ff05c5827d9a6c080923c97292b27427f8c17d
BLAKE2b-256 checksum
How to use checksums
58e571b312cc37cfe6d61b8d5848a29c27723ac6abac1bbc70ed4af8fc2b6a47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

Release files / django_translation_flags-1.0.6-py3-none-any.whl

Download URL django_translation_flags-1.0.6-py3-none-any.whl
Size 159.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9ea9fbbe206212c3509a366acb5a86407b5b5e4b5b1d3cfb49b8e7cc774b9577
BLAKE2b-256 checksum
How to use checksums
9072edbf0589f391b0ad98313e7cb52a8fbb6d1be6b4219fddba414fca103a1c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

Security for django-translation-flags 1.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page