Internationalization Django

pip install pipenv
mkdir django_translate
cd django_translate
pipenv shell
pipenv install django
django-admin startproject translate_project .
python manage.py startapp translate_app
translate_project/settings.py...INSTALLED_APPS = [
...
'translate_app.apps.TranslateAppConfig,
...
]
LANGUAGES = (
('en','English'),
('pl', 'Polish'),
)
MIDDLEWARE = [
...
#The order of middleware is very important
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale/'),
)
...
translate_app/views.pyfrom django.views.generic import TemplateViewclass HomeView(TemplateView):
template_name = 'translate_app/home.html'
templates/
translate_app/
home.html
locale/
en/
pl/
translation_project/urls.py...
from django.conf.urls.i18n import i18n_patterns
from translation_app import views
urlpatterns = i18n_patterns(
...
path('', views.HomeView.as_view(), name='home'),
)
django-admin makemessages --all
django-admin compilemessages
...#: .\flat_app\templates\flat_app\home.html:58
msgid "Album"
msgstr "Album"
#: .\flat_app\templates\flat_app\home.html:88
msgid "Album example"
msgstr "Przykładowy album"
#: .\flat_app\templates\flat_app\home.html:89
msgid ""
"Something short and leading about the collection below—its contents, the "
"creator, etc. Make it short and sweet, but not too short so folks don’t "
"simply skip over it entirely."
msgstr ""
"Trochę krótkiego tekstu na wstępniaku w jaki sposób zebrać treść znajdującą się poniżej, "
"twórcę, etc. Stwórz go krótki i przyjazny dla oka, ale nie za krótki aby czytelnik nie "
"przeoczył go w trakcie przeglądania strony. "
...
mkdir dj_translate
cd dj_translate
pipenv shell
pipenv install django
pipenv install django-parler
django-admin startproject translate_project .
python manage.py startapp translate_app
translate_project/settings.py...INSTALLED_APPS = [
...
'translate_app.apps.TranslateAppConfig,
'parler',
...
]
LANGUAGES = (
('en','English'),
('pl', 'Polish'),
)
PARLER_LANGUAGES = {
None: (
{'code': 'en'},
{'code': 'pl'},
),
'default': {
'fallback': 'en',
'hide_untranslated': False,
}
}
MIDDLEWARE = [
...
#The order of middleware is very important
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale/'),
)
...
translate_app/model.pyfrom django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
from parler.models import TranslatableModel, TranslatedFields
class Post(TranslatableModel):
translations = TranslatedFields(
title = models.CharField(_('title'), max_length=250, db_index=True),
body = models.TextField(_('body'), db_index=True),
)

author = models.ForeignKey(User,
on_delete=models.CASCADE,
related_name='blog_posts',
verbose_name=_('author'),)
publish = models.DateTimeField(_('publish'),default=timezone.now)
created = models.DateTimeField(_('created'),auto_now_add=True)
updated = models.DateTimeField(_('updated'),auto_now=True)
def __str__(self):
return self.title
translate_app/admin.pyfrom django.contrib import admin
from .models import Post
from parler.admin import TranslatableAdmin
@admin.register(Post)
class PostAdmin(TranslatableAdmin):
list_display = ('title','author','publish')
list_filter = ('created','publish','author')
search_fields = ('title','body')
templates/
blog/
base.html
post/
detail.html
list.html
django-admin makemessages --all
#This is for our template translation ni locale folder
django-admin compilemessages
from django.views.generic import ListView, DetailView
from .models import Post
class PostListView(ListView):
model = Post
template_name = 'blog/post/list.html'
context_object_name = 'posts'
class PostDetail(DetailView):
model = Post
template_name = 'blog/post/detail.html'
context_object_name = 'post'
from django.contrib import admin
from django.urls import path
from django.conf.urls.i18n import i18n_patterns
from inter_app import viewsurlpatterns = i18n_patterns(
path('admin/', admin.site.urls),
path('', views.PostListView.as_view(),name='home'),
path('post/<int:pk>/', views.PostDetail.as_view(), name='post-detail'),
)
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser

--

--

--

web developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Singleton pattern in Python

AIRDROP Testnet Guide — KYVE

Consistent hashing with bounded loads, using a Red-Black Tree

PowerDirector Mod Apk No Watermark Download

PowerDirector Mod Apk No Watermark Download

Python virtualenv shell prompts

Czech Mothers, What Do you Cook From?

How to Get started with MongoDB?

HackTheBox Writeup — Fuse

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pawel Grajewski

Pawel Grajewski

web developer

More from Medium

What is Django?

Django Tutorial: Getting started.

A Map App in Django using Folium | Python

How to create desktop notifications in Django?

[Django] Django development notes (2) Templates