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