Reputation:
I have a website with multiple languages. However, these languages should NEVER be chosen by the visitor and instead are either a) decided based on the domain name (same site runs under different domains, one for each language), or b) retrieved from the database, depending on the exact page opened.
From the manual I understand I should be using the django.utils.translation.activate() function to explicityly set the language. However, I am not having any luck.
If I am correct, this is the code I need (no need for me to set cookies, so this is what I isolated):
from django.utils import translation
translation.activate("fr")
Is this right? Where exactly should I place this? Can I use the sites framework to link a language to a particular language? Here is what I have tried in my views.py file, but it isn't working. No error - just no translation. Is there a way I can debug this to figure out where it goes wrong?
# views.py
from .models import *
from django.shortcuts import render
from django.utils import translation
from django.conf import settings
# I am trying to activate a default setting for the site, based on the domain of the site
if settings.SITE_ID == 1:
translation.activate("fr")
else:
translation.activate("en")
def homepage(request)
return render(request, "index.html")
def listing(request, id)
listing = Listing.objects.get(pk=id)
language = listing.language
translation.activate(language) # For particular pages, I need a different language, based on a value from the database. This is what I tried.
return render(request, "index.html")
Upvotes: 0
Views: 2617
Reputation: 3242
According to your use case, the best place to use translation.activate()
is in some middleware, since the language is site/domain dependant.
When you are doing:
# views.py
from .models import *
from django.shortcuts import render
from django.utils import translation
from django.conf import settings
# I am trying to activate a default setting for the site, based on the domain of the site
if settings.SITE_ID == 1:
translation.activate("fr")
else:
translation.activate("en")
The call is made when the module is parsed, not when a HTTP request is made and processed.
With a middleware, the proper locale can be selected like this:
# middlewares.py
def site_locale_middleware(get_response):
def middleware(request):
if request.site.id == 1:
translation.activate("fr")
elif request.site.id == 2:
translation.activate("es")
else:
translation.activate("en")
response = get_response(request)
return response
return middleware
And add this in your settings:
MIDDLEWARES = [
"django.contrib.sites.middleware.CurrentSiteMiddleware""",
# your middleware should be placed after CurrentSiteMiddleware
# so that request.site is available
"yourproject.middlewares.site_locale_middleware",
]
This will ensure you have a proper language setup for each site, and you can override it when needed with:
def listing(request, id)
listing = Listing.objects.get(pk=id)
language = listing.language
with translation.override(language):
# using override here is important, because it means it will only override
# the language when the response is rendered, and revert back to the previous language afterwards
return render(request, "index.html")
Upvotes: 1