Package: django-translation-flags
Django Translation FlagsThis 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.RequirementsDjango Translation Flags require Django Internationalization and
localization properly configured. You can see more about these settings
inhttps://docs.djangoproject.com/en/2.1/topics/i18n/Basically you need to:Define a customLANGUAGESlist onsettings.pywith tuples,
i.e:fromdjango.utils.translationimportgettext_lazyas_LANGUAGES=[('de',_('German')),('en',_('English')),('pt-br',_('Brazilian Portuguese'))]Only languages listed in theLANGUAGESsetting can be selected. This
example restricts languages that are available for automatic selection
to German, English and Brazilian PortugueseAdd MiddlewareTo 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',]Markup the text to translation:The format of.pofiles is straightforward. Each.pofile
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:fromdjango.utils.translationimportgettext_lazyas__("Welcome to my site.")…thendjango-adminmakemessageswill have created a.pofile
containing the following snippet – a message:#: path/to/python/module.py:23
msgid "Welcome to my site."
msgstr ""Generate and compile it using the commands bellow:The first step is to create a message file for a new language:django-adminmakemessages-lde-len-lpt_BRCompiling message files after creating your message file:django-admincompilemessagesFor 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-filesInstallInstall from PyPI:pip install django-translation-flagsConfigurationAdddjango-translation-flagsto your list ofINSTALLED_APPSin
settings.py:INSTALLED_APPS=[...'django_translation_flags',...]Add the Django Translation Flags URLs tourls.py:fromdjango.conf.urlsimporturl,includeurlpatterns=[...path('i18n/',include('django_translation_flags.urls')),...]Inject the required meta tags in yourbase.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 tosquare:{% 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>Thelanguagestemplate tags accept**kwargsto configure the
class to HTML tags. So you can set the classes to these HTML tags:li_class: Class tolitag (Default: empty)a_class: Class toatag (Default: empty)The HTML structure is:<li><a><span></span></a></li>How does it work?The Django Translation Flags has aCSSfile 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 andzh-hant: Traditional Chinese.The App get the language code fromLANGUAGESonsettings.pyand
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 moduledjango.conf.locale.LANG_INFOLANG_INFO is a dictionary structure to
provide meta information about languages.FeedbackFeedback and pull requests are strongly encouraged and kindly
appreciated (-:ContributingPythonClone the repository.Create a virtualenv with Python 3.6 or 3.7Active the virtualenv.Install the dependencies.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.pyLess to CSSYou will need ofnodeandnpmpreviously installed.Install the dependenciesRun the gulpEdit the less files:/assets/less/npm install
npm run buildLicensingAll files in this repository are distributed under the MIT license.