How to make the urls with and without a language prefix work in Django i18n?
Asked Answered
B

1

0

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:

enter image description here

And, http://localhost:8000/en/app1/ can show Hello as shown below:

enter image description here

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:

enter image description here

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:

enter image description here

But, http://localhost:8000/fr/app1/ doesn't work to show Bonjour as shown below:

enter image description here

So, how can I show Bonjour with both http://localhost:8000/fr/app1/ and http://localhost:8000/app1/?

Balas answered 23/5, 2023 at 0:9 Comment(0)
B
0

In core/urls.py, you should add fr/ to admin and app1 paths in urlpatterns, then add i18n_patterns() set admin and app1 paths and prefix_default_language=False to 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 = [
          ↓↓↓ Here
    path('fr/admin/', admin.site.urls),
    path('fr/app1/', include('app1.urls')),
]         ↑↑↑ Here 

urlpatterns += i18n_patterns( # Here
    path('admin/', admin.site.urls),
    path('app1/', include('app1.urls')),
    prefix_default_language=False
)

Then, http://localhost:8000/fr/app1/ can show Bonjour as shown below:

enter image description here

Also, http://localhost:8000/app1/ can show Bonjour as shown below:

enter image description here

And, http://localhost:8000/en/app1/ can show Hello as shown below:

enter image description here

Balas answered 23/5, 2023 at 0:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.