0
votes

I am trying to internationalize a Django dictionary application using the Django translation system. I have successfully translated most of the content on the site but I am having problems translating URL patterns, with the exception of the admin page.

My goal is to change the language of both content and URL based on prefix language code in the URL. For example:

www.example.com/en/dictionary/define    // (content in English)
www.example.com/it/dizionario/definisci // (content in Italian)
www.example.com/fr/dictionnaire/définis // (content in French)
...

I checked the Django documentation on this (https://docs.djangoproject.com/en/3.1/topics/i18n/translation/#translating-url-patterns) but found it quite vague and could not really understand it.

project-level urls.py:

from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import gettext_lazy as _

from dictionary import views as dictionary_views

urlpatterns = [
    path('admin/', admin.site.urls),
]

dictionary_patterns = ([
    path('', dictionary_views.index, name='index'),
    path(_('define/'), dictionary_views.define, name='define'),
    path(_('define/<slug:headword_slug>'), dictionary_views.headword, name='headword'),
], 'dictionary')

urlpatterns += i18n_patterns(
    path(_('dictionary/'), include(dictionary_patterns, namespace='dictionary')),
)

As for app-specific urls.py, I have no clue on what to do there. Should I import the dictionary_patterns variable from above and add it to the app's urlpatterns list? Should I delete the app's urls.py file altogether?

Can anybody help?

1

1 Answers

1
votes

Just try to add import include in the urlpatterns like

In the project/urls.py

from django.conf.urls import include

path('en/dictionary/',include('app URL here with extension.url'),
path('it/dizionario/',include('app URL here with extension.url'),
path('fr/dictionnaire/',include('app URL here with extension.url'),

In the Appname/urls.py

   urlpatterns = [
   url(r'^define$/',views.name of view,name=''),
   url(r'^definisci/$',views.name of view,name=''),
   url(r'^définis/$',views.name of view,name=''),

Check this out: https://docs.djangoproject.com/en/3.1/topics/http/urls/