我希望我的网站的默认语言是波斯语,而不是英语! 并且 DEFAULT_LANGUAGE 不适合我!
我的settings.py 文件的一部分:
LANGUAGES = (
('fa','Persian'),
('en','English'),
)
DEFAULT_LANGUAGE = 1
LANGUAGE_CODE = 'fa'
TIME_ZONE = 'Asia/Tehran'
USE_I18N = True
USE_L10N = True
USE_TZ = True
我也读过这些,但它们没有帮助:
Django 如何在 i18n_patterns 中设置默认语言?
请帮助我。谢谢!
在settings.py中你可能有这个中间件,它使用默认语言作为你的浏览器语言。
'django.middleware.locale.LocaleMiddleware'
'django.middleware.locale.LocaleMiddleware'
中间件将默认语言作为浏览器的语言,正如@Alasgar提到的那样。- {YOUR_APP}/middleware/ - __init__.py - languages.py
language.py
内放入以下代码:
def SetDefaultLangMiddleware(get_response):
def middleware(request):
# Code to be executed for each request before processing it
if not request.COOKIES.get('django_language'): # Language not selected by the user ?
# Set your default language code here
request.COOKIES['django_language'] = 'ar'
response = get_response(request)
# If you want to process anycode after the view is called
return response
return middleware
'django.middleware.locale.LocaleMiddleware'
:这是最快、最简单的解决方案。就我而言,Django==“3.1” https://github.com/yifaneye/django-default-language