Programatically reverse an url to other language

86 views
Skip to first unread message

Jorge Cardoso Leitão

unread,
May 15, 2014, 2:24:41 PM5/15/14
to django...@googlegroups.com
Hi. 

I'm trying to reverse urls to other languages and I'm having some difficulties:

Consider a new project in python 3.3 Django 1.6.4 with the adittional settings

          LOCALE_PATHS = (BASE_DIR + '/locale',)
LANGUAGE_CODE = 'en-us'
LANGUAGES = (
    ('en-us', 'English'),
    ('de', 'German'),
)

The following views.py:

def home(request):
    from django.utils import translation
    from django.utils.translation import ugettext as _
 
    translation.activate('de')
    print(reverse("about"))
    print(_('search'))
              render(request, 'home.html') 

          def about(request):
              render(request, 'about.html')

and the following urls.py:

urlpatterns = patterns('',
                       url(r'^$', views.home, name='home'),
                       url(r'^%s$' % _('about'), views.about, name='about'),

After running make_messages, translate the strings in .po, compile_messages and restart the server,
going to / prints the following in the console:

>> /about
>> [translation of "search"]

I.e. even though "about" is translatable, it is not being translated; my objective is to have 
it translated to a german url.

I tried going as far as using 

    from django.conf import settings
    urlconf = settings.ROOT_URLCONF
    resolver = RegexURLResolver(r'^/', urlconf)

    print(resolver._reverse_with_prefix(about, '/'))

or even

    temp = __import__(urlconf, globals(), locals(), [], 0)
    print(getattr(temp.urls, "urlpatterns"))

but they are all giving me the untranslated url.

I digged into the core.urlresolvers.py, and it RegexURLPattern.regex is always returning the en-us pattern, even when the language_code is 'de'.

I then went to see why, and it seems the urls.py are always being imported in the en version, even after the translation.activate('de') call.

Does anyone know if this is the intended behavior, or if there a way to change it?

Thanks a lot,
Jorge

Tomas Ehrlich

unread,
May 15, 2014, 3:09:18 PM5/15/14
to django...@googlegroups.com
Hi,
use ugettext_lazy for translation of urls, models and other stuff which
is loaded before language is determined (it's usually done in
Middleware or later in view, otherwise all strings will be translated
into default language).

You can also write:

url(_(r'^about$'), views.about, name='about'),

which is easier to read and you can see in your .po file, that you are
translating url.


Cheers,
Tom


Dne Thu, 15 May 2014 11:24:41 -0700 (PDT)
Jorge Cardoso Leitão <jorgeca...@gmail.com> napsal(a):
signature.asc

Jorge Cardoso Leitão

unread,
May 17, 2014, 4:14:38 AM5/17/14
to django...@googlegroups.com
Hi,

You were right that I was using ugettext instead of ugettext_lazy.
Thank you for your response and for your suggestions on the code.

Regards,
Jorge
Reply all
Reply to author
Forward
0 new messages