2017-06-01 111 views
0

我試圖創建一個網站,將是法語和英語,但我的Django的語言環境並沒有真正的工作:我的機器上,它工作正常(與./manage.py runserver爲什麼我的django locales失敗了?

The site as rendered by the latest Google Chrome on the latest MacOS while the server is on my machine

但是當我把我的服務器上的代碼,部署...

The site as rendered by the same Google Chrome and the same MacOS, but while on my server

這就是網頁代碼: 模板

<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作品,因爲當我改變郎用手在設置LANGUAGE_CODE它的.py ...

所以,我真的不知道該從何而來,我很高興我任何人都可以照一些光。我知道由於LANGUAGE_CODE沒有設置,它會默認爲en-us,但這是我想要的:網站是英文的,除非有法文網頁瀏覽器的人來。

+0

是安裝在服務器上一樣,你有你的開發機器上的語言環境中的語言環境? – matyas

+0

@matyas是,這兩個區域/ FR和區域/ EN上的回購,並編制了服務器上 –

回答

0

調整了一下後,我得到了一些作品,但不知道如何我把它想:

我在settings.py改變了MIDDLEWARE VAR來MIDDLEWARE_CLASSES,我加入國際化模式,以我的網址:

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

然而,這是不是真的是我想要的,因爲我希望能夠使所有的工作而不觸及的URL。

如果有人知道的方式做到這一點,我會接受它,但我會這樣的問題:現在。