python - Django internationalization / translation not working for dynamically generated urls -
i've run issue dual language site can't find answer in other questions. so, have wired form , display system relies heavily on verbose names given each model. verbose name translated, idea work base. so, example, have model in products/models.py:
from django.utils.translation import ugettext_lazy _ class producttype(models.model): description = models.charfield(max_length=250) ... class meta: verbose_name = _('Ürün tipi') verbose_name_plural = _('Ürün tipleri')
from there, create class-based forms , views. then, create urls importing views , looping through them in urls.py (i focus on 1 url example):
from django.conf.urls import patterns, url .views import editing_views editing_patterns = [] view in editing_views: list_view = view['list'] slug = slugify(list_view.model._meta.verbose_name) editing_patterns.append( url(_(r'{0}/list/$').format(slug), list_view.as_view(), name='{0}-list'.format(slug)) ) ... urlpatterns = patterns('products.views', *editing_patterns )
as can see, url , url name both coming slugified version of model's verbose name, translated. when site set english, works fine. when set turkish, system not finding translated urls:
reverse 'urun-tipi-list' arguments '()' , keyword arguments '{}' not found. 0 pattern(s) tried: []
now strange part when import url patterns file django shell, turkish names, regardless of language site set to:
<regexurlpattern urun-tipi-list urun-tipi/list/$>
now, perhaps wonkiness django shell, printed patterns console @ end of urls.py file, , main problem seems urls in runtime in english, regardless of site set to:
product-type/list/$
but verbose_name translation working, because getting urun-tipi-list getting printed in {% url %} tag of template not being found.
i'll throw in more general information internationalization setup in case helpful.
in settings.py:
template_context_processors = ( ... 'django.core.context_processors.i18n', ... ) middleware_classes = ( ... 'django.middleware.locale.localemiddleware', ... ) use_i18n = true languages = [ ('tr', _('turkish')), ('en', _('english')), ] locale_paths = ( 'locale', )
and after running command line stuff, resulting .po files have following particular verbose name:
/en
#: products/models.py:14 msgid "Ürün tipi" msgstr "product type"
/tr
#: products/models.py:14 msgid "Ürün tipi" msgstr "Ürün tipi"
very mysterious if ask me, appreciated.
Comments
Post a Comment