Jump to main content

Django

Our goal is to create a more connected internet without language barriers.

In an increasingly connected world, the language barrier should not be an obstacle to sharing knowledge and building global communities. With Translate Projects, you can easily globalize your projects, making your content accessible to people all over the world.

Initial configuration Django

  • You can create a basic project in Django. official documentation

  • If you have a project, let's move forward....

Implementation of translations

Set up i18n

  • You must have the django.conf.i18n package configured.
settings.py
#Default language
LANGUAGE_CODE = 'es'

#Languages to translate
# import `gettext_lazy` to avoid translation error
from django.utils.translation import gettext_lazy as _

LANGUAGES = [
('es', _('Spanish')),
('en', _('English')),
# ('hi', _('Hindi')),
# ('fr', _('French')),
# ('de', _('German')),
# ('zh', _('Chinese')),
# ('ko', _('Korean')),
# ('ja', _('Japanese')),
# ('ru', _('Russian')),
# ('it', _('Italian')),
]

#Enable the package `django.conf.i18n`
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

TRANSLATE_PROJECTS_API_KEY = '#'
TRANSLATE_PROJECTS_EXCLUDE_DIRS = ["env/*"]
Important
  • Don't forget to ignore your development environment folder, for example env/.
  • You must have the django.conf.locale package configured.

We create the locale folder where the results of our translations will be stored.

Terminal
mkdir locale

This is how we will end up with the following directory structure.

settings.py
├── db.sqlite3
├── django_tutorial
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── locale # route of our translations
└── manage.py

We add the path of our translations to our list of paths in our main yourproject/settings.py file.

settings.py

# import `os`
import os

LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale'),
]
  • We added the localization middleware django.middleware.locale.
settings.py
MIDDLEWARE = [
Your other middlewares...,
'django.middleware.locale.LocaleMiddleware',
]
  • You must have the django.conf.urls.i18n package configured.

We must add the i18n/ route to our list of routes in our main yourproject/urls.py file.

urls.py
from django.conf.urls.i18n import i18n_patterns
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('i18n/', include('django.conf.urls.i18n')),
]

Set up the path for our templates.

We created the templates folder where our templates will be stored.

Terminal
mkdir templates

This is how we will end up with the following directory structure.

settings.py
.
├── db.sqlite3
├── django_tutorial
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── locale
├── manage.py
└── templates # route of our templates

Inside this folder, we will add our HTML templates for our project.

settings.py
# import `os`
import os
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], #Path of our templates
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

Automate translations

We must install the package translate-projects-django in our project.

pip install translate-projects-django

Register package in settings.py

settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
Your other packages...,
'translate_projects_django', #add the package
]
Create account

🚀 Automate and optimize your translations.

To make the most of Translate Projects, you need an API Key. Sign up with no obligation.

  • We do not ask for a credit card to try Translate Projects.

Benefits of obtaining your API Key

  • Smart Management: Sync and automatically manage the translations of your projects.
  • Exclusive Administrative Panel: Customize and oversee all your translations from a centralized interface.
  • Priority Support: Receive quick and specialized technical assistance when you need it.
  • Accurate Translations: Our technology understands the context of your code to provide accurate translations.
  • 30,000 Welcome Eniacs: Test all the features without obligation with internal tokens that enhance your translations.

Sign up now and get your API Key to unlock the full potential of Translate Projects!

Create account now→

Add API key

It's very simple; you just need to add your API key in your project's settings.

settings.py
TRANSLATE_PROJECTS_API_KEY = '#' #<- Your API key

Execute the translations.

We open our command console and run the translate command so that our translations are executed.

Terminal
python manage.py translate

If everything is correct, you should see something like this:

Run translations

If you need to translate to a specific single language, you can do so with the --locale option as follows:

Terminal
python manage.py translate --locale es

The translations will be stored in the locale folder.

Terminal
.
├── db.sqlite3
├── django_tutorial
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── locale
│ ├── en
│ │ └── LC_MESSAGES
│ │ ├── django.po
│ │ └── django.po
│ └── es
│ └── LC_MESSAGES
│ ├── django.mo
│ └── django.po
├── manage.py
└── templates
Support

We are here to help you.

If you have any questions, need help, or want to contribute, feel free to contact me. I am here to support you with whatever you need.

Support the Project

If this project has been useful to you, I invite you to buy me a coffee. Your support helps me continue improving and creating valuable content. Thank you for being here! ☕

Buy me a coffee

How to add translations?

Adding translations to our templates is very easy and quick.

  • We are going to create a basic template.
Terminal
touch templates/base.html

I will explain the structure of our template part by part and then I will leave the complete file.

templates/base.html
#{{ importamos `i18n` para poder usar las traducciones}}
{% load i18n %}

<!DOCTYPE html>

#{{ obtenemos el idioma actual y los idiomas disponibles}}
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}

#{{ definimos el idioma actual}}
<html lang="{{ LANGUAGE_CODE }}">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}Translate Projects{% endblock %}</title>
</head>
<body>
  • We added the form to change the language.
templates/base.html
<body>

{% comment %} Basic form to change language {% endcomment %}
<form method="post" action="{% url 'set_language' %}">
{% csrf_token %}

{% for code, name in LANGUAGES %}
<input
type="radio"
name="language"
value="{{ code }}"
{% if code == LANGUAGE_CODE %}checked{% endif %}
>
{{ name }}

{% endfor %}
<input type="submit" value="Submit">
</form>
  • We add the texts we need to translate with the trans function.
templates/base.html
    {% comment %} Traducciones {% endcomment %}
<h1>{% trans "Mi primera traducción" %}</h1>

<h2>{% trans "Este es un texto traducido, siguenos en YouTube!" %}</h2>
</body>
</html>

Complete example

  • We are going to add the following to our template.
templates/base.html
{% load i18n %}
<!DOCTYPE html>
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}

<html lang="{{ LANGUAGE_CODE }}">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}Translate Projects{% endblock %}</title>
</head>
<body>

{% comment %} Basic form to change language {% endcomment %}
<form method="post" action="{% url 'set_language' %}">
{% csrf_token %}

{% for code, name in LANGUAGES %}
<input
type="radio"
name="language"
value="{{ code }}"
{% if code == LANGUAGE_CODE %}checked{% endif %}
>
{{ name }}

{% endfor %}
<input type="submit" value="Submit">
</form>

{% comment %} Traducciones {% endcomment %}
<h1>{% trans "Mi primera traducción" %}</h1>

<h2>{% trans "Este es un texto traducido, siguenos en YouTube!" %}</h2>

</body>
</html>

We carry out the creation of translations.

We carry out the creation of translations so that our translations are made.

Terminal
python manage.py translate

If everything is correct, you should see something like this again.

Run translations

Result of our translations

This would be our website's result.

Translation results