DEV Community

Cover image for How to create a translate project with Django
Abdulla Fajal
Abdulla Fajal

Posted on

How to create a translate project with Django

This article will create a translator app using Django, Django is a popular Python web framework. This app will allow users to input text in one language and receive a translation in another language. We will be using the translate python package to do the translation. If you want to learn Python and Django, then our courses are available, you can see them. Let us begin without wasting your time at all.

I think Django will be already installed in your system If not then you can install it like this:

pip install django
Enter fullscreen mode Exit fullscreen mode

And Install the translate package:

pip install translate
Enter fullscreen mode Exit fullscreen mode

Now let's create our project:

django-admin startproject translator
Enter fullscreen mode Exit fullscreen mode

A project named movie will be created, you can do cd movie and go inside it.

cd translator
Enter fullscreen mode Exit fullscreen mode

You have to create an app, you can give any name to the project and app, we have given it the name of app.

python manage.py startapp app
Enter fullscreen mode Exit fullscreen mode

Now you have to mention this app in your project setting in INSTALLED_APPS

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "app", # new
]
Enter fullscreen mode Exit fullscreen mode

Create views that handle the translation. Now we need to create a view that will handle the translation. Open up app/views.py and add the following code:

from django.shortcuts import render
from translate import Translator

# Create your views here.


def home(request):
    if request.method == "POST":
        text = request.POST["translate"]
        to_lang = request.POST["tolanguage"]
        from_lang = request.POST["fromlanguage"]
        translator = Translator(to_lang=to_lang, from_lang=from_lang)
        translation = translator.translate(text)

        context = {
            "translation": translation,
        }
        return render(request, "home.html", context)
    return render(request, "home.html")
Enter fullscreen mode Exit fullscreen mode

This code defines a view function called home() which is associated with the route for the homepage ("/") in a Django web application.

The function starts by checking the request method. If the request method is "POST", the function extracts data from the form submitted through the POST method. The text variable contains the text entered in the form, the to_lang variable contains the target language selected in the form and the from_lang variable contains the source language selected in the form.
A new instance of the Translator class from the translate module is then created, passing in the to_lang and from_lang variables as parameters. The translate() method of the Translator instance is then called with text as the argument to translate the text. The translated text is then stored in the translation variable.

Finally, a dictionary named context is created, which contains the translated text as a value associated with the key "translation". The home() function then renders the "home.html" template, passing in the context dictionary as a context variable.

If the request method is not "POST", the function simply renders the "home.html" template without any context variable.

We have to create the template mentioned in the views, and for that, we have to create a directory named templates inside our app, in that, we will write the code of the template.

Read More Here

Top comments (0)