2012-12-13 53 views
0

在看到嘗試了所有關於此主題的建議後,我的翻譯仍然無法正常工作。Django翻譯總是以英語呈現

/settings.py文件

# Language code for this installation. All choices can be found here: 
# http://www.i18nguy.com/unicode/language-identifiers.html 
LANGUAGE_CODE = 'en-us' 

LOCALE_PATHS = (
    '/Users/samb/Projects/transtest/locale' 
) 

# Custom Languages 
ugettext = lambda s: s 

LANGUAGES = (
    ('de', ugettext('German')), 
    ('en', ugettext('English')), 
    ('fr', ugettext('French')), 
    ('fr-CA', ugettext('French Canadian')), 
) 

SITE_ID = 1 

# If you set this to False, Django will make some optimizations so as not   
# to load the internationalization machinery. 
USE_I18N = True                 

# If you set this to False, Django will not format dates, numbers and 
# calendars according to the current locale. 
USE_L10N = True 

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.locale.LocaleMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    # Uncomment the next line for simple clickjacking protection: 
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware', 
) 

view.py

from django.shortcuts import render_to_response 
from django.template import RequestContext 


def trans(request): 
    return render_to_response('index.html', context_instance=RequestContext(request)) 

我的模板文件(index.html的)

{% load i18n %} 
{% get_current_language as LANGUAGE_CODE %} 
{% get_available_languages as LANGUAGES %} 
<html> 
    <head> 
     <title>translations</title> 
    </head> 
    <body> 
     {% for l in LANGUAGES %} 
      {{ l }}<br /> 
     {% endfor %} 
     {{ LANGUAGE_CODE }}<br /> 
     {% trans "Welcome to my site" %} 
    </body> 
</html> 

PO文件(已編譯) 位於/ Users/samb/Projects/transtest/locale/fr/LC_MESSAGES

#: transtest/templates/index.html:13 
msgid "Welcome to my site" 
msgstr "Please work" 

我永遠無法獲得'歡迎到我的網站'上班。我的模板中的LANGUAGES和LANGUAGE_CODE變量都工作(除非我'Accept_language:fr_CA')。

在閱讀了關於這個主題的所有其他帖子後,仍然有同樣的問題,我覺得我必須有一個愚蠢的錯誤,或者完全失去了一個重要的一步。有什麼想法嗎?

更新:這是我正在測試的翻譯:

telnet localhost 8000 
Connected to localhost. 
Escape character is '^]'. 
GET/
Accept_language: fr 
<html> 
    <head> 
     <title>Translations</title> 
    </head> 
    <body> 

      (&#39;de&#39;, u&#39;Allemand&#39;)<br /> 

      (&#39;en&#39;, u&#39;Anglais&#39;)<br /> 

      (&#39;fr&#39;, u&#39;Fran\xe7ais&#39;)<br /> 

      (&#39;fr-CA&#39;, u&#39;French Canadian&#39;)<br /> 

     fr<br /> 
     Welcome to my site 
    </body> 
</html> 
Connection closed by foreign host. 

我注意到,語言會被翻譯,但「歡迎來到我的網站」不是。

回答

1

文檔說here是LocaleMiddleware ...

...應該來SessionMiddleware後,因爲LocaleMiddleware使得 使用會話數據。它應該在CommonMiddleware 之前發佈,因爲CommonMiddleware需要激活的語言才能 解析所請求的URL。

也許它會幫助您在MIDDLEWARE_CLASSES的定義中考慮到這一點。

+0

感謝您的回覆!我確實忽視了這一點,但卻做出了改變,但無濟於事。我開始猜測我的測試方法。我所做的只是通過telnet來創建一個GET請求,並且通過Accept_language:fr ...來查看吐出來的東西......也許有更好的方法呢? – sambev

+0

這解決了我的問題。謝謝!我發誓我讀了那部分文檔,但仍然犯了這個錯誤。我一定很累! – shailenTJ