Django 1.5:从Django i18n中的网址中删除默认语言前缀

时间:2019-02-20 10:10:37

标签: django django-1.5 django-i18n

我正在使用 Django 1.5

我在项目中设置了国际化,并在 settings.py 文件中设置了以下变量

LANGUAGE_CODE = 'en'

LANGUAGES = (
    ('en', 'English'),
    ('es', 'Spanish')
)

urls.py 文件内容

urlpatterns += i18n_patterns('',
    url(r'^', include(app_urlpatterns)),
)

但是这会将 zh_CN 附加到每个URL,例如

https://example.com/en/dashboard

我想在网址格式中隐藏默认语言。

我尝试将prefix_default_language=False放在url模式中

urlpatterns += i18n_patterns('',
    url(r'^', include(app_urlpatterns)),
    prefix_default_language=False
)

但这给了错误

i18n_patterns() got an unexpected keyword argument 'prefix_default_language'

如何在Django 1.5中从URL隐藏默认语言前缀?

2 个答案:

答案 0 :(得分:1)

据我对docs的了解,默认情况下 django 1.5 不支持prefix_default_language。因此,您要么需要放弃使用它的想法,要么需要自己实现它。如果选择后一个选项,则需要创建一个新的i18n_patterns函数,还需要在其中更新LocaleRegexURLResolver。例如:

from django.core.urlresolvers import LocaleRegexURLResolver


class CustomLocaleRegexURLResolver(LocaleRegexURLResolver):
     def __init__(self, urlconf_name, default_kwargs=None, app_name=None, namespace=None, prefix_default_language=True):
        self.prefix_default_language = prefix_default_language
        super(LocaleRegexURLResolver, self).__init__(None, urlconf_name, default_kwargs, app_name, namespace)

     @property
     def regex(self):
        language_code = get_language()
        if language_code == settings.LANGUAGE_CODE and not self.prefix_default_language:
             return re.compile('^')
        if language_code not in self._regex_dict:
            regex_compiled = re.compile('^%s/' % language_code, re.UNICODE)
            self._regex_dict[language_code] = regex_compiled
        return self._regex_dict[language_code]

现在,您需要在自定义CustomLocaleRegexURLResolver中使用i18n_patterns,如下所示:

from django.conf import settings
from django.conf.urls import patterns, url
from django.core.urlresolvers import LocaleRegexURLResolver

def custom_i18n_patterns(prefix, *args, *kwargs):
    prefix_default_language = kwargs.get('prefix_default_language', True)
    pattern_list = patterns(prefix, *args)
    if not settings.USE_I18N:
        return pattern_list
    return [CustomLocaleRegexURLResolver(pattern_list, prefix_default_language=prefix_default_language)]

最后,在您的网址中使用它:

urlpatterns += custom_i18n_patterns('',
    url(r'^', include(app_urlpatterns)),
    prefix_default_language=False
)

已根据代码available here完成了实现。

答案 1 :(得分:0)

您应该花两秒钟的时间在设置文件中设置USE_I18N = True。然后Django将进行一些优化,以免加载国际化机制。在'django.core.context_processors.i18n'设置中添加TEMPLATE_CONTEXT_PROCESSORS

settings.py

USE_I18N = True
TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages"
)

urls.py

urlpatterns += (
    url(r'^', include(app_urlpatterns)),
)

参考:https://django-doc-test1.readthedocs.io/en/stable-1.5.x/topics/i18n/translation.html#overview