13
Translated URLs with Django 1.4 Django User Group Berlin #15, July 12, 2011

Translated URLs

Embed Size (px)

Citation preview

Page 1: Translated URLs

Translated URLswith Django 1.4

Django User Group Berlin #15, July 12, 2011

Page 2: Translated URLs

• URLs with a locale code

http://example.com/de/

• URLs with a translated path

http://example.com/über-uns/

• URLs with both

http://example.com/de/über-uns/

What are translated URLs exactly?

Page 3: Translated URLs

• Unique URLs for each language

• Search engines index all content

• Explicit language detection

• Ugly URLs are ugly, e.g. ?lang=de

Why?

Page 4: Translated URLs

URLs with a locale code

Page 5: Translated URLs

from django.conf.urls.defaults import patterns, urlfrom django.conf.urls.i18n import i18n_patterns

urlpatterns = patterns('foo.views' url(r'^$', 'home', name='homepage'),)

urlpatterns += i18n_patterns('foo.views', url(r'^about$', 'about', name='about'), url(r'^news/(?P<id>\d+)$', 'news', name='news')),)

URIs with a locale code

Page 6: Translated URLs

>>> from django.core.urlresolvers import reverse>>> from django.utils.translation import activate>>> >>> activate('en')>>> reverse('home')'/'>>> reverse('about')'/en/about'>>>>>> activate('nl')>>> reverse('news', kwargs={'id': '123'})'/nl/news/123'

URIs with a locale code

Page 7: Translated URLs

URLs with atranslated path

Page 8: Translated URLs

from django.conf.urls.defaults import patterns, urlfrom django.utils.translation import ugettext_lazy as _

urlpatterns = patterns('foo.views' url(r'^$', 'home', name='homepage'), url(_(r'^about$'), 'about', name='about'), url(_(r'^news/(?P<id>\d+)$'), 'news', name='news')),)

URIs with a translated path

Page 9: Translated URLs

>>> from django.core.urlresolvers import reverse>>> from django.utils.translation import activate>>> >>> activate('de')>>> reverse('home')'/'>>> reverse('about')u'/über-uns'>>>>>> reverse('news', kwargs={'id': '123'})'/neuigkeiten/123'

URIs with a translated path

Page 10: Translated URLs

URLs with a locale code and a translated path

Page 11: Translated URLs

from django.conf.urls.defaults import patterns, urlfrom django.conf.urls.i18n import i18n_patternsfrom django.utils.translation import ugettext_lazy as _

urlpatterns = patterns('foo.views' url(r'^$', 'home', name='homepage'),)

urlpatterns += i18n_patterns('foo.views', url(_(r'^about$'), 'about', name='about'), url(_(r'^news/(?P<id>\d+)$'), 'news', name='news')),)

URIs with a locale code and a translated path

Page 12: Translated URLs

>>> from django.core.urlresolvers import reverse>>> from django.utils.translation import activate>>> >>> activate('en')>>> reverse('home')'/'>>> reverse('about')'/en/about'>>>>>> activate('de')>>> reverse('about')'/de/über-uns'>>> reverse('news', kwargs={'id': '123'})'/de/neuigkeiten/123'

URIs with a locale code and a translated path

Page 13: Translated URLs

h [email protected]

d @jezdez

oW Jannis Leidel

Questions?

Slides willbe sharedon Twitter