Django 网址未被翻译

Django urls aren't being translated

Translating URL patterns 之后,我可以让我的 url 以活动语言为前缀,但我无法翻译它们。

urls.py

from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import ugettext_lazy as _

from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings

from exercises.views import ExerciseListView

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
]

urlpatterns += i18n_patterns(
   ...
   url(_(r'^exercises/$'), ExerciseListView.as_view(), name='list'),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

locale/es/LC_MESSAGES/django.po

#: myproject/urls.py:54
msgid "^exercises/$"
msgstr "^ejercicios/$"

manage.py shell

>>> from django.core.urlresolvers import reverse
>>> from django.utils.translation import activate
>>> activate('en')
>>> reverse('list')
'/en/exercises/'
>>> activate('es')
>>> reverse('list')
'/es/exercises/'          <---- should be /es/ejercicios as translated in .po

如何让 reverse('list') 显示 '/es/ejercicios/' ?

问题不仅在于 URL,还在于消息文件中的所有翻译字符串都没有被翻译。

具有以下目录结构:

-Project  #base directory
  -apps
  -templates
  -project
     -settings.py
  -locale
     -es
       -LC_MESSAGES
          -django.po

只需将 LOCALE_PATHS 配置添加到 settings.py 即可解决问题

LOCALE_PATHS = (
    'locale',
)

Django will look within each of these paths for the /LC_MESSAGES directories containing the actual translation files.

*在 Django 1.8 中测试