我有一个现有的多语言Django应用程序,我正在移植到Django 1.4。 I18n支持目前基于一些丑陋的黑客,我想使用Django的内置i18n模块。
一个限制是我不想更改已经存在的网址。这个网站已经活跃了一段时间,并且有一些外部链接,我不想打破或重定向。网址方案的工作方式如下:英文内容位于网站的根目录,而其他语言则使用域名中的前缀:
英文网址:
/
/articles/
/suggestions/
西班牙语网址:
/es/
/es/articulos/
/es/sugerencias/
我已经将翻译后的页面与Django 1.4的i18n模块配合使用了,但它确实非常非常希望将所有英文网址放在/ en /下。我尝试了一些不同的黑客,包括为英文版定义非国际化的网址:
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)
这会使网址正确解析,但执行反向解析的{%url%}标记不起作用。
完成非前缀英文网址的最佳方法是什么?
答案 0 :(得分:15)
我使用solid-i18n-url来解决类似的问题: https://github.com/st4lk/django-solid-i18n-urls
很好的描述如何在这里工作: http://www.lexev.org/en/2013/multilanguage-site-django-without-redirects/
答案 1 :(得分:11)
Django 1.10原生支持它。正如他们在文档中所说:
i18n_patterns( * urls,prefix_default_language = True )
此功能可用于根URLconf,Django会自动将当前活动语言代码添加到
i18n_patterns()
中定义的所有网址模式。将prefix_default_language设置为False会从默认语言(
LANGUAGE_CODE
)中删除前缀。这在向现有网站添加翻译时非常有用,这样当前网址就不会发生变化。
来源:https://docs.djangoproject.com/en/1.10/topics/i18n/translation/#language-prefix-in-url-patterns
答案 2 :(得分:10)
更新:阅读下面的答案,Django 1.10原生支持
我遇到了这个问题并以这种方式解决了:
创建了一个替代i18n_patterns
,它不以网站主要语言为前缀(在settings.LANGUAGE_CODE
中定义)。
创建了一个替代中间件,它只使用URL前缀语言来激活当前语言。
我没有看到使用这种技术的任何副作用。
代码:
# coding: utf-8
"""
Cauê Thenório - cauelt(at)gmail.com
This snippet makes Django do not create URL languages prefix (i.e. /en/)
for the default language (settings.LANGUAGE_CODE).
It also provides a middleware that activates the language based only on the URL.
This middleware ignores user session data, cookie and 'Accept-Language' HTTP header.
Your urls will be like:
In your default language (english in example):
/contact
/news
/articles
In another languages (portuguese in example):
/pt/contato
/pt/noticias
/pt/artigos
To use it, use the 'simple_i18n_patterns' instead the 'i18n_patterns'
in your urls.py:
from this_sinppet import simple_i18n_patterns as i18n_patterns
And use the 'SimpleLocaleMiddleware' instead the Django's 'LocaleMiddleware'
in your settings.py:
MIDDLEWARE_CLASSES = (
...
'this_snippet.SimpleLocaleMiddleware'
...
)
Works on Django >=1.4
"""
import re
from django.conf import settings
from django.conf.urls import patterns
from django.core.urlresolvers import LocaleRegexURLResolver
from django.middleware.locale import LocaleMiddleware
from django.utils.translation import get_language, get_language_from_path
from django.utils import translation
class SimpleLocaleMiddleware(LocaleMiddleware):
def process_request(self, request):
if self.is_language_prefix_patterns_used():
lang_code = (get_language_from_path(request.path_info) or
settings.LANGUAGE_CODE)
translation.activate(lang_code)
request.LANGUAGE_CODE = translation.get_language()
class NoPrefixLocaleRegexURLResolver(LocaleRegexURLResolver):
@property
def regex(self):
language_code = get_language()
if language_code not in self._regex_dict:
regex_compiled = (re.compile('', re.UNICODE)
if language_code == settings.LANGUAGE_CODE
else re.compile('^%s/' % language_code, re.UNICODE))
self._regex_dict[language_code] = regex_compiled
return self._regex_dict[language_code]
def simple_i18n_patterns(prefix, *args):
"""
Adds the language code prefix to every URL pattern within this
function, when the language not is the main language.
This may only be used in the root URLconf, not in an included URLconf.
"""
pattern_list = patterns(prefix, *args)
if not settings.USE_I18N:
return pattern_list
return [NoPrefixLocaleRegexURLResolver(pattern_list)]