Implementing additional languages on Django website
Using Candy Translate is the easiest way to provide more languages on the Django website. Just follow these 8 simple steps and localization will be ready in few moments
We recommend to start with:
Instruction video with example:
YouTube
Source code of example implementation:
GitHub
1. Download Translations template with as many languages as you want to use for your website
Just go to CandyTranslate.com and open the tab GET TEMPLATE. Mark the languages you want to use on your website and click DOWNLOAD
2. Fill the template with the text content of your website
Every text that you are using on your website and want to have translated put into separate row of your Excel template (column C). For each row add the name of the page where text is used and any unique name of this field (columns A and B).
Send the file to translators to fill remaining columns
3. Generate the CANDY file that will keep all information about your website content
Go to the CandyTranslate.com and open the tab GENERATE TRANSLATION.
Provide your filled template.xls file and you will receive candy.py.
Put this file to your Django project folder and add this line of code on the top of files urls.py and views.py:
views.py :
from . import candy
urls.py :
from translate_app import candy
4. Create urls to all your translated pages
In the file urls.py change “path” to “*candy.path” as in the example below. Please change it for all pages that will be localized
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name = 'home'),
path('contact', views.contact, name='contact')
]
urlpatterns = [
path('admin/', admin.site.urls),
*candy.path('', views.home, name = 'home'),
*candy.path('contact', views.contact, name='contact')
]
5. Make your code to generate translated pages
In the file views.py change “render” to “candy.render” as on the example below.
Please change it in all functions that should generate translated content
def home(request):
context={
}
return render(request, 'home.html', context)
def contact(request):
context={
}
return render(request, 'contact.html', context)
def home(request):
context={
}
return candy.render(request, 'home.html', context)
def contact(request):
context={
}
return candy.render(request, 'contact.html', context)
Now you need to change all texts on all translated pages to variables with the names corresponding to columns A and B joined with “_”. Close the name in double curly brackets. So if in excel file you had for your text “home” in column A and “title” in column B then replace this text with {{home_title}}. Check the example:
<h1>This is Title of my home page</h1>
<p>This is paragraph of text on my home page to welcome users</h1>
<h1>{{home_title}}</h1>
<p>{{home_welcome}}</p>
After this step you should be able again use your website in your main language. You can also manually open translated pages by adding language to the end of link. Eg for opening Spanish(es) page just add “/es” like that:
www.myPage.com/es
6. Make all your links to open the correct language version
Just find all your {% url … %} links and remove the quotation for the names of link and add “candyLink.”. Example:
{% url "home" %}
{% url "contact" %}
{% url candyLink.home %}
{% url candyLink.contact %}
Now all your links should open correct language version of linked page!
7. Add the option for users to switch between available languages
With Candy Translate this is only 1 line of code. Just add this line (usually in the <header> )
{{languageMenu|safe}}
8. Make your translated pages visible and friendly to search engines like Google
Having your website SEO-friendy is very important. We need to change the language of your pages and add linking between different languages. We can achieve it by introducing small code change and adding 1 line of code (in <head> ). See below:
<html lang="en">
<head>
<html lang="{{lang}}">
<head>
{{languageReferences|safe}}
If you want, Candy Translate can also generate the sitemap.xml file for you with … 1 line of code! Just add this line to your urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
*candy.path('', views.home, name = 'home'),
*candy.path('contact', views.contact, name='contact'),
*candy.sitemap()
]
SUMMARY:
This is it! The whole process of implementing these changes can take just few minutes.
For any further changes in texts or translations or even adding more languages, development will not be needed. Just make changes in Excel file and generate your new candy.py
Additional commands that may be helpful
1. Using your translations outside of templates
You can also easily use your translated texts within your view functions.
Example of using function translated(request, translationVariable):
def index(request):
context={
"home_title2": candy.translated(request, "home_title") + "!!!"
}
return candy.render(request, 'index.html', context)
2. redirect to correct language version
You can use candy.redirect to always open a correct version of your page
def index(request):
if request.user.is_superuser:
redirect("adminPanel")
def index(request):
if request.user.is_superuser:
candy.redirect(request, "adminPanel")