django i18n_patterns hide default lang_code from url

后端 未结 5 1981
旧时难觅i
旧时难觅i 2020-12-13 22:38

I\'m using the i18n_patterns to add a prefix of current lang_code to my url.

urlpatterns += i18n_patterns(\'\',
    url(r\'^\', include(\'blaszczakphoto2.gal         


        
5条回答
  •  心在旅途
    2020-12-13 23:01

    Django >=1.10 can handle this natively. There is a new prefix_default_language argument in i18n_patterns function.

    Setting prefix_default_language to False removes the prefix from the default language (LANGUAGE_CODE). This can be useful when adding translations to existing site so that the current URLs won’t change.

    Source: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#language-prefix-in-url-patterns

    Example:

    # Main urls.py:
    urlpatterns = i18n_patterns(
        url(r'^', include('my_app.urls', namespace='my_app')), 
        prefix_default_language=False
    )
    
    # my_app.urls.py:
    url(r'^contact-us/$', ...),
    
    # settings:
    LANGUAGE_CODE = 'en' # Default language without prefix
    
    LANGUAGES = (
        ('en', _('English')),
        ('cs', _('Czech')),
    )
    

    The response of example.com/contact-us/ will be in English and example.com/cs/contact-us/ in Czech.

提交回复
热议问题