Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

internationalization - Django i18n default language without path prefixes

I have an existing multi-lingual Django app that I'm porting to Django 1.4. I18n support is currently based on some ugly hacks, I'd like to make it use Django's built-in i18n modules.

One constraint is that I don't want to change the urls that are already in place. This site has been active for a while and there are external links to it that I dont' want to break or redirect. The urls scheme works like this: English content is at the root of the site, while other languages use prefixes in the domain name:

English urls:

/
/articles/
/suggestions/

Spanish urls:

/es/
/es/articulos/
/es/sugerencias/

I've got the translated pages working with Django 1.4's i18n modules, but it really, really, really wants to put all the English urls under /en/. I've tried a few different hacks, including defining non-internationalized urls for the English version:

def build(callback):
  return callback('',
    url(_(r'^$'), home.index, name="home"),
    url(_(r'^articles/$'), content.article_list, name='article_list'),
    url(_(r'^suggestions/$'), suggestions.suggestions, name='suggestions'),
  )

urlpatterns = build(patterns)
urlpatterns += build(i18n_patterns)

This makes the urls resolve properly, but the {% url %} tag to do reverse resolution doesn't work.

What's the best way to accomplish non-prefixed English-language urls?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I used solid-i18n-url to solve similar problem: https://github.com/st4lk/django-solid-i18n-urls

Nice description how it works located here: http://www.lexev.org/en/2013/multilanguage-site-django-without-redirects/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...