2016-09-03 116 views
0

我創建了一個簡單的網站來測試國際化,但我無法按照自己想要的方式工作。我想在我的views.py中更改消息,而不檢查request.LANGUAGE_CODE(顯示正確)。Django國際化(I18N)不改變文字

我可以去的網址前綴/EN//PT-BR/,但他們不改變在模板上的文字。

我試圖運行

django-admin makemessages --locale=pt_BR 

我改了行

#: mytest/views.py:7 
msgid "Welcome to my site." 
msgstr "Bem vindo ao meu site." 

django-admin compilemessages --locale=pt_BR 

PS:(即使它是錯的,我試過Django的管理員makemessages/compilemessages --locale = pt-br)

settings.py改變什麼(加我的應用程序,添加語言環境中間件,加入了一些內在的設置)

INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'mytest' 
] 
MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.locale.LocaleMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
] 
# Internationalization 
# https://docs.djangoproject.com/en/1.10/topics/i18n/ 

LOCALE_PATHS = [ 
    os.path.join(BASE_DIR, 'locale/translations/'), 
] 

LANGUAGE_CODE = 'en-us' 

from django.utils.translation import ugettext_lazy as _ 
LANGUAGES = [ 
    ('pt-br', _('Portuguese')), 
    ('en', _('English')), 
] 

TIME_ZONE = 'UTC' 
USE_I18N = True 
USE_L10N = True 
USE_TZ = True 

views.py

from django.shortcuts import render 
from django.utils.translation import ugettext_lazy as _ 

def index(request): 
    print(request.LANGUAGE_CODE) #this shows correctly the prefix in the url 
    output = _("Welcome to my site.") 
    context = {"test_translate": output} 
    return render(request, "mytest/index.html", context) 

urls.py

from django.conf.urls.i18n import i18n_patterns 
from mytest import views 

urlpatterns = [ 
] 
urlpatterns += i18n_patterns(
    url(r'^$', views.index, name='index'), 
) 

回答

0

我想我的路徑是不正確的。我相信額外的斜線是錯誤的...我從LOCALE_PATH中刪除/翻譯/它現在正在工作。

LOCALE_PATHS = [ 
    os.path.join(BASE_DIR, 'locale'), 
] 

然後我跑

django-admin compilemessages -l pt_BR 

修改的* .po生成並運行

django-admin compilemessages -l pt_BR 

我也改名爲EN-USENLANGUAGE_CODE = 'en-us'