我试图创建一个法语和英语的网站,但我的Django语言环境并没有真正起作用:在我的机器上,它工作正常(使用./manage.py runserver
)
但是当我在我的服务器上发送代码时,进行部署......
这是该网页的代码: 模板
<head>
<!-- Removed meta and useless stuff for clarity -->
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
<!-- LANGUAGE_CODE is always 'en-us' on prod -->
<!-- Which is the default in settings.py -->
{% get_available_languages as LANGUAGES %}
{% language fav_lang %}
<!-- But this is empty on prod, while it is 'en' on my machine -->
</head>
<body>
<!-- Same here, removed CSS stuff and pretty spacing -->
BetaGames
<br>
{% trans "message" %}
<br>
{{ fav_lang }}
</body>
{% endlanguage %}
并查看:
from django.template import loader
from django.http import HttpResponse
def index(request):
template = loader.get_template("index.html")
context = {
'fav_lang': request.LANGUAGE_CODE,
}
#So yeah, request.LANGUAGE_CODE is empty on prod
return HttpResponse(template.render(context, request))
我几乎从那里想到,这肯定是我的服务器配置破坏的东西。但是我的服务器settings.py
和我的本地测试是相同的:
import os
from django.utils.translation import ugettext_lazy
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Just removed debug/hosts/secret key there
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware', #locale should be between sessions and common
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'BetaGames.urls' #Just have / pointing to the "website in construction" view
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates/'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.i18n', #Needed here, think the docs said that
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'BetaGames.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), #Nothing in the DB yet, so this is default stuff
}
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
USE_TZ = True
TIME_ZONE = 'Europe/Paris'
USE_I18N = True
USE_L10N = True
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
os.path.join(BASE_DIR, 'static/spectre/docs/dist'), #CSS stuff
]
LANGUAGES = (
('fr', ugettext_lazy('French')),
('en', ugettext_lazy('English')),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
我有.po文件并将它们编译为.mo并且我知道traduction有效,因为当我在settings.py中用LANGUAGE_CODE手动更改lang时会这样做...
所以我真的不知道这是从哪里来的,我很高兴我可以对此有所启发。
PS:我知道由于没有设置LANGUAGE_CODE,它将默认为en-us,但这就是我想要的:网站是英文的,除非有人带着法国网络浏览器。
答案 0 :(得分:0)
稍微调整一下后,我得到了一些有用的东西,但不是我想要的东西:
我在settings.py中将MIDDLEWARE
var更改为MIDDLEWARE_CLASSES
,并将i18n模式添加到我的网址:
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls.i18n import i18n_patterns
import pages
urlpatterns = []
urlpatterns += i18n_patterns(
url(r'^$', pages.views.index),
url(r'^admin/', admin.site.urls),
)
现在我可以访问法语翻译网站domain.com/fr/app
和domain.com
重定向到domain.com/en
。
但是,这并不是我想要的,因为我希望能够在不触及网址的情况下完成所有工作。
如果有人知道如何做到这一点,我会接受它,但我现在就推动它。