Django - Translation (Create multilingual websites using Django)

 

When we visit the other countries web sites we first face an issue with languages, absolutely we have google to convert into our country language and vise verse but what if we give the choice to user choose there own language, So this tutorial talks about how to create a multilingual website in Django. 


SetUp:

  • USE_I18N = True
  • LANGUAGE_CODE = 'en-us'

USE_I18N tends to translation is activated or allowed, and LANGUAGE_CODE is the default language for your project you can change based on your choice.


a) create LOCALE_PATHS variable in settings.py,

LOCALE_PATHS = [
    os.path.join(BASE_DIR, 'locale')
]

Which means there are the translations files(.po or .mo ) in locale folder
 

 

b) ln the .html file load the i18n, to use the translation tag 


{% load i18n %}
<h1>{% trans 'from html'%}</h1>


c) you can do translation from views.py file also, import gettext as _,  and use this with a key which you want to convert, as below 


from django.utils.translation import gettext as _

def index(request):
    text = _('Hello from views')
    return render(request, 'index.html', context={'text': text})

load this context in .html file as {{text}}

 


 

Translations:

To do translation we need  to run 2 commands 
 

  • django-admin makemessages -l fi
  • django-admin compilemessages
    
    

Before running this don't forget to create an empty folder with the named 'locale' at the application level.

1) makemessages -l fi: here fi is Finnish language code, and makemessages command creates .po file where we can define our translation for that language, as shown below 


#: localization/views.py:7
msgid "Hello from views"
msgstr "Hei näkymistä"

#: templates/index.html:10
msgid "from html"
msgstr "HTML-tiedostosta"

as you can see msdid is actual string and msgstr is the transltated format for that.



 

2) Compilemessages: this command is used to translate the actual words, vai creating.mo file in the same directory 


3) Ways of Translations:

a) Static way :
b) Dynamic (runtime) :

 

a) Static way: 

This part is straightforward,  we have to just define translated language code in settings.py. So it will automatically load specified values that are provided while creating the .po file. 
 

LANGUAGE_CODE = 'en-us'   # this default we have to overide the code here 
LANGUAGE_CODE = 'fi'   # we have fi translated files so I used fi here. 


b) Dynamic way

To do a dynamic way, just modify add one line urls.py and load all languages in .html so you can choose the specific language, 
once you choose the language, set it using the inbuilt URL 'set_language' as below 

urls.py 


urlpatterns = [
    path('i18n/', include('django.conf.urls.i18n')),  # it helps to set languge 
    url(r'^admin/', admin.site.urls),
    path('', index)
]

 

.html


<form action="{% url 'set_language'%}" method='post'>{% csrf_token%}
   <input type="hidden" name="text" value="{{ redirect_to }}">
   <select name="language" id="">
      {% get_available_languages as LANGUAGES %}
      {% get_language_info_list for LANGUAGES as languages %}
      {% for language in languages %}
         <option value="{{language.code}}" {% if language.code == LANGUAGE_CODE %} selected {% endif %}>
            {{language.name_local}} {{language.code}}
         </option>
      {% endfor %}
   </select>
   <input type="submit" value="Go">
</form>

 

That's all about the dynamic way of change. but here the problem is it will load all the language which are present in Django as below 


To filter out or provide the specific languages you can give options in settings.py 


LANGUAGES = [
    ('en', 'English'),
    ('fi', 'Finnish'),
    ('hi', 'Hindi'),
]

 

Now in the drop-down, it shows only 3 languages. 

Awesome! now you can also create multilingual web pages in Django. 
for code Github 

Thank you !!!
Happy Pythoning... 


© 2020 Blog, All rights reserved.   This work is licensed under  Creative Commons License
Top