使用django 1.4的url语言环境

时间:2012-09-01 11:19:08

标签: django localization

我正在尝试应用django URL语言环境,如此处所述

https://docs.djangoproject.com/en/dev/topics/i18n/translation/#translating-url-patterns

下面是我的设置文件

LANGUAGE_CODE = 'en'

gettext = lambda s: s

LANGUAGES = (
    ('ar', gettext('Arabic')),
    ('en', gettext('English')),
)

我的urls.py如下

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.i18n import i18n_patterns



admin.autodiscover()


urlpatterns = patterns('',
    url(r'^sitemap\.xml$', 'sitemap.view', name='sitemap_xml'),
)

urlpatterns += i18n_patterns('',

    url(r'^', include('bon_app.apps.bonz.urls')),

    url(r'^', include('bon_app.apps.pages.urls')),

)

它目前只适用于英语,当我尝试127.0.0.1/en/它可以工作,但当我尝试127.0.0.1//ar/它不知道为什么会发生这种情况?

目前正在使用django 1.4

更新

bon_app /应用/ bonz / urls.py

from django.conf.urls import patterns, include, url


urlpatterns = patterns('bon_app.apps.bonz.views',

    url(r'^$',              'home',                               name='bonz-home'),
    url(r'^promotion/$',    'home',  {'model'   : 'Promotion'},   name='bonz-promo'),
    url(r'^event/$',        'home',  {'model'   : 'Event'},       name='bonz-event'),
    url(r'^directory/$',    'directory',                          name='bonz-directory')
)

bon_app /应用/页/ urls.py

from django.conf.urls import patterns, include, url


urlpatterns = patterns('bon_app.apps.pages.views',

    url(r'^contact-us/$', 'contact_us', name='pages-contactus'),
    url(r'^about-us/$', 'about_us', name='pages-aboutus'),

)

2 个答案:

答案 0 :(得分:1)

你说:

  

它目前只适用于英语,当我尝试127.0.0.1/en/它可以工作,但当我尝试127.0.0.1//ar/它不知道为什么会发生这种情况?

你在“ar”之前有一个双斜杠“//”而不是一个斜杠“/".

也许那已经是问题了?

答案 1 :(得分:0)

您必须将LocaleMiddleware添加到MIDDLEWARE_CLASSES