如何在Django中翻译url?

oyt4ldly  于 2023-06-25  发布在  Go
关注(0)|答案(1)|浏览(128)

这是我的django-project下面从英语翻译成法语。* 我使用Django 4.2.1

django-project
 |-core
 |  |-settings.py
 |  └-urls.py
 |-my_app1
 |  |-views.py
 |  └-urls.py
 |-my_app2
 └-locale
    └-fr
       └-LC_MESSAGES
          |-django.po
          └-django.mo

下面是core/settings.py

# "core/settings.py"

MIDDLEWARE = [
    ...
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

from django.utils.translation import gettext_lazy as _

LANGUAGES = (
    ('en', _('English')),
    ('fr', _('French'))
)

并且,gettext()用于将Test转换为my_app1/views.py中的Examen,如下所示:

# "my_app1/views.py"

from django.shortcuts import render
from django.utils.translation import gettext as _

def test(request):     # ↓ Here ↓
    return HttpResponse(_("Test"))

并且,test()hello/world/路径设置为my_app1/urls.py中的urlpatterns,如下所示:

# "my_app1/urls.py"

from django.urls import path
from . import views

app_name = "my_app1"

urlpatterns = [
        # ↓ ↓ Here ↓ ↓
    path("hello/world/", views.test, name="test")
]

并且,my_app1my_app1/路径设置为urlpatterns,i18n_patterns()如下所示:

# "core/urls.py"

from django.urls import path, include
from django.conf.urls.i18n import i18n_patterns

urlpatterns = i18n_patterns(
        # ↓ Here ↓
    path("my_app1/", include('my_app1.urls'))
)

并且,locale/fr/LC_MESSAGES/django.po中的"English""French""Test"分别设置为"Anglais""Français""Examen",如下所示:

# "locale/fr/LC_MESSAGES/django.po"

...

#: .\core\settings.py:140
msgid "English"
msgstr "Anglais" # Here

#: .\core\settings.py:141
msgid "French"
msgstr "Français" # Here

#: .\my_app1\views.py:5
msgid "Test"
msgstr "Examen" # Here

...

然后,我可以从英语翻译成法语,如下所示:

http://localhost:8000/fr/my_app1/hello/world/

现在,我如何将上面的英语网址翻译成下面的法文网址?

http://localhost:8000/fr/mon_app1/bonjour/monde/
wz8daaqr

wz8daaqr1#

您应该使用gettext_lazy()并将"hello/world/"替换为_("hello/world/"),如下所示。* 对于路径,gettext_lazy()可以工作,但gettext()不起作用,你可以看到我的答案解释了如何在Django中翻译:

# "my_app1/urls.py"

from django.urls import path
from . import views
from django.utils.translation import gettext_lazy as _ # Here

app_name = "my_app1"

urlpatterns = [
        # ↓ ↓ ↓ Here ↓ ↓ ↓
    path(_("hello/world/"), views.test, name="test")
    # path("hello/world/", views.test, name="test")
]

然后,使用gettext_lazy()并将"my_app1/"替换为_("my_app1/"),如下所示:

# "core/urls.py"

from django.urls import path, include
from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import gettext_lazy as _ # Here

urlpatterns = i18n_patterns(
        # ↓ ↓ Here ↓ ↓
    path(_("my_app1/"), include('my_app1.urls'))
    # path("my_app1/", include('my_app1.urls'))
)

然后,运行下面的命令:

django-admin makemessages -l fr

然后,分别为"my_app1/""hello/world/"添加"mon_app1/""bonjour/monde/",如下所示:

# "locale/fr/LC_MESSAGES/django.po"

...

#: .\core\settings.py:140
msgid "English"
msgstr "Anglais"

#: .\core\settings.py:141
msgid "French"
msgstr "Français"

#: .\core\urls.py:20
msgid "my_app1/"
msgstr "mon_app1/" # Here

#: .\my_app1\urls.py:8
msgid "hello/world/"
msgstr "bonjour/monde/" # Here

#: .\my_app1\views.py:5
msgid "Test"
msgstr "Examen"

...

然后,运行下面的命令:

django-admin compilemessages

现在,您可以将英语url翻译为法文url,如下所示:

http://localhost:8000/fr/mon_app1/bonjour/monde/

另外,我不知道如何在Django Admin中翻译URL,所以我问了the question

相关问题