Django网址没有被翻译

时间:2015-12-06 00:14:37

标签: python django django-i18n django-multilingual

关注Translating URL patterns,我可以让我的网址以活动语言为前缀,但我无法翻译它们。

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)

区域设置/ 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

如何让反向('列表')显示'/ es / ejercicios /'

1 个答案:

答案 0 :(得分:1)

问题不仅在于网址,而且邮件文件中的所有翻译字符串都没有被翻译。

具有以下目录结构:

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

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

LOCALE_PATHS = (
    'locale',
)
  

Django将在每个路径中查找   / LC_MESSAGES包含实际的目录   翻译文件。

*在Django 1.8中测试