This is my django-project as shown below. *I'm learning Translation with Django 4.2.1:
django-project
|-core
| |-settings.py
| └-urls.py
|-app1
| |-models.py
| |-admin.py
| └-urls.py
|-app2
└-locale
└-en
└-LC_MESSAGES
|-django.mo
└-django.po
And, this is core/settings.py which sets fr to LANGUAGE_CODE as a default language code as shown below:
# "core/settings.py"
MIDDLEWARE = [
# ...
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
# ...
]
# ...
LANGUAGE_CODE = 'fr' # Here
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
from django.utils.translation import gettext_lazy as _
LANGUAGES = (
('fr', _('Français')),
('en', _('Anglais')),
)
LOCALE_PATHS = [
BASE_DIR / 'locale',
]
And, this is app1/views.py which returns the default french word Bonjour from test view as shown below:
# "app1/views.py"
from django.http import HttpResponse
from django.utils.translation import gettext as _
def test(request): # Here
return HttpResponse(_("Bonjour"))
And, this is app1/urls.py with the path set test view in urlpatterns as shown below:
# "app1/urls.py"
from django.urls import path
from . import views
app_name = "app1"
urlpatterns = [
path('', views.test, name="test") # Here
]
And, this is core/urls.py with i18n_patterns() set admin and app1 paths in urlpatterns as shown below:
# "core/urls.py"
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.i18n import i18n_patterns
urlpatterns = i18n_patterns(
path('admin/', admin.site.urls),
path('app1/', include('app1.urls'))
)
Then, http://localhost:8000/fr/app1/ can show Bonjour as shown below:
And, http://localhost:8000/en/app1/ can show Hello as shown below:
Now, I also want to show Bonjour with http://localhost:8000/app1/ but it redirects to http://localhost:8000/en/app1/ showing Hello as shown below:
Actually, if I set prefix_default_language=False to i18n_patterns() in core/urls.py as shown below:
# "core/urls.py"
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.i18n import i18n_patterns
urlpatterns = i18n_patterns(
path('admin/', admin.site.urls),
path('app1/', include('app1.urls')),
prefix_default_language=False # Here
)
Then, http://localhost:8000/app1/ can show Bonjour as shown below:
But, http://localhost:8000/fr/app1/ doesn't work to show Bonjour as shown below:
So, how can I show Bonjour with both http://localhost:8000/fr/app1/ and http://localhost:8000/app1/?






