python - i18n and l10n in Django with different views -


i'm trying localize application internationalization purposes, i'm having problem developing url views once language chosen.

i have these relevant entries in settings.py

...  use_i18n = true  use_l10n = true  locale_paths = (     os.path.join(base_dir, 'locale'), )  django.utils.translation import ugettext_lazy _  languages = (     ('en', _('english')),     ('es', _('spanish')),     ('zh', _('chinese')), )  ... 

and urls.py looks this...

# -*- coding: utf-8 -*-  django.conf.urls import include, url, patterns django.contrib import admin django.conf import settings django.conf.urls.i18n import i18n_patterns   urlpatterns = patterns('',      url(r'^$', 'myapp.views.home', name='home'),     url(r'^admin/', include(admin.site.urls)),     url(r'^downloads/$', 'myapp.views.downloads'),     # etc. etc. )  urlpatterns += i18n_patterns('',     url(r'^$', 'myapp.views.home', name='home'),     url(r'^admin/', include(admin.site.urls)),     url(r'^downloads/$', 'myapp.views.downloads'),     # etc. etc. ) 

so "works" locally. go localhost:8080/es/ , see spanish page. however, when click on application's button navigate downloads page, go localhost:8080/downloads in english. if type localhost:8080/es/downloads, unfound url error.

how can make more naturally localized , internationalized? if there's information missing, please let me know. i'll add it.

i'm using django 1.7.4.

thanks, erip

edit

from home downloads, have html looks this...

  <a class="navbar-brand" href="/"> {% trans "home"%}</a>   <a class="navbar-brand" href="/downloads/"> {% trans "downloads"%}</a> 

have added django.middleware.locale.localemiddleware middleware_classes (docs)?

middleware_classes = (            'django.contrib.sessions.middleware.sessionmiddleware',           'django.middleware.locale.localemiddleware',         'django.middleware.common.commonmiddleware',  ) 

also, shouldn't define patterns twice. patterns wish handled i18n should defined in i18n_patterns(...). if identical, block enough:

# -*- coding: utf-8 -*-  django.conf.urls import include, url, patterns django.contrib import admin django.conf import settings django.conf.urls.i18n import i18n_patterns  urlpatterns = i18n_patterns('',     url(r'^$', 'myapp.views.home', name='home'),     url(r'^admin/', include(admin.site.urls)),     url(r'^downloads/$', 'myapp.views.downloads'),     # etc. etc. ) 

update: building url in template

in url pattern add name attribute downloads pattern:

urlpatterns = i18n_patterns('',     # ...     url(r'^downloads/$', 'myapp.views.downloads', name='downloads'),     # ... ) 

in template, resolve link url tag:

<a class="navbar-brand" href="{% url 'home' %}">{% trans "home"%}</a> <a class="navbar-brand" href="{% url 'downloads' %}">{% trans "downloads"%}</a> 

this way django builds url respecting current language.


how debug if resolving works correctly

for debugging django resolves url to, try following in shell.

$ ./manage.py shell  >>> django.core.urlresolvers import reverse >>> django.utils.translation import activate >>> activate('en') >>> reverse('downloads') '/en/downloads'     >>> activate('es') >>> reverse('downloads') '/es/downloads/' 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -