Opened 7 years ago

Closed 5 years ago

Last modified 5 years ago

#11585 closed New feature (fixed)

i18n in urls.py

Reported by: digi604 Owned by: Jannis Leidel
Component: Internationalization Version: master
Severity: Normal Keywords:
Cc: patrick.lauber@…, osirius@…, jonas.obrist@…, info@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Because search engines not only search in english we normally "translate" the urls as well to get higher pageranks in the sites languages.
This is very cumbersome at the moment with django.

example:

en: /contact/
de: /kontakt/
it: /contatto/

they all point to the same view.

it would be nice to have a i18n_url() that would resolve based on the current language.

i18n_url({'de':r'^kontakt/', 'en':r'^contact/', 'it':r'^contatto/'}, contact, name="contact"}

it would be nice to be able to tell reverse() and {% url %} which language to resolve.

{% url contact de %}

reverse('contact', language="de")

If no language is defined it should take the current one from get_language()

Attachments (1)

i18nurls.diff (34.3 KB) - added by Orne Brocaar 5 years ago.
Implementation for language-prefix and internationalization of URL patterns

Download all attachments as: .zip

Change History (18)

comment:1 Changed 7 years ago by Alex Gaynor

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedDesign decision needed

comment:2 Changed 7 years ago by Thejaswi Puthraya

Component: UncategorizedInternationalization

comment:3 Changed 7 years ago by Jannis Leidel

milestone: 1.21.3

Moving to 1.3 since this is certainly a new feature and we are in bugfix only mode.

comment:4 Changed 6 years ago by anonymous

Great idea. It also requires adding the language code to the URL to ensure that the URL is unique for each language. So the example must create the following URLs:

/en/contact/

/de/kontakt/

/it/contatto/

comment:5 Changed 6 years ago by Jannis Leidel

This is somewhat related to #9333, which proposes adding a note about django-localeurl to the documentation.

comment:6 Changed 6 years ago by Robert Lujo

Can this be used (it doesn't report error on my place):

    (r'^%s/%s/...$' % (settings.LANGUAGE_CODE, _(u"contact")), view ...

it should produce translated and prefixed urls like:

  • /en-us/contact/...
  • /de/kontakt/...

Or you need more general solution?

comment:8 Changed 6 years ago by Sam Bull

Cc: osirius@… added

I'm the maintainer of transurlvania. I think it solves the problem raised in this ticket. I'd like more feedback before I propose including it wholesale as a patch.

comment:9 Changed 6 years ago by Jannis Leidel

milestone: 1.3
Triage Stage: Design decision neededAccepted

Removing the 1.3 milestone for now, since I don't see a chance to get this ready in time. But for what it's worth, I think adopting a few of transurlvania's tools and/or document the technique is worthwhile for future releases.

comment:10 Changed 5 years ago by Julien Phalip

Severity: Normal
Type: New feature

comment:11 Changed 5 years ago by Orne Brocaar

Easy pickings: unset
Owner: changed from nobody to Orne Brocaar
Status: newassigned
UI/UX: unset

comment:12 Changed 5 years ago by Jonas Obrist

Cc: jonas.obrist@… added

Changed 5 years ago by Orne Brocaar

Attachment: i18nurls.diff added

Implementation for language-prefix and internationalization of URL patterns

comment:13 Changed 5 years ago by Orne Brocaar

Cc: info@… added
Has patch: set
milestone: 1.4
Owner: changed from Orne Brocaar to Jannis Leidel
Status: assignednew

I have added a patch which contains the following:

  • Implementation of language-prefixing (and detection by LocaleMiddleware) and translation of URL patterns
  • Documentation update
  • Unittests

comment:14 Changed 5 years ago by Jannis Leidel

Resolution: fixed
Status: newclosed

In [16405]:

Fixed #11585 -- Added ability to translate and prefix URL patterns with a language code as an alternative method for language discovery. Many thanks to Orne Brocaar for his initial work and Carl Meyer for feedback.

comment:15 Changed 5 years ago by Jakub Roztočil

Thanks for this new feature.

Are there any plans to include also an i18n-aware version of the {% url %} template tag? I know that it will work fine for the current language, but when one needs a URL of a page in another language, then the existing tag doesn't help. It would be great if there was also an option to get a URL of the current page in another language without having to specify it or its name as well. I'm aware of django.views.i18n.set_language() but a link is often needed for search engines to be able to discover the page.

This is a general problem applying also to reverse() but there I suppose you can work it around by activating another language, reversing the URL and then switching back.

I'm willing to provide a patch for the template tag if there is any chance for it being included in the core.

comment:16 Changed 5 years ago by Aymeric Augustin

Yes, this is #16332, and it's already implemented.

comment:17 Changed 5 years ago by Jacob

milestone: 1.4

Milestone 1.4 deleted

Note: See TracTickets for help on using tickets.
Back to Top