Django 国际化(I18N)不改变文本
Django Internationalization (I18N) not changing text
我创建了一个简单的网站来测试国际化,但我无法让它按我想要的方式工作。我想更改 views.py 中的消息而不检查 request.LANGUAGE_CODE(显示正确)。
我可以访问前缀为 /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-admin 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'),
)
我认为我的路径不正确。我相信额外的斜线是错误的...我从 LOCALE_PATH 中删除了 /translations/ 并且它现在可以工作了。
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale'),
]
那我运行
django-admin compilemessages -l pt_BR
修改生成的*.po和运行
django-admin compilemessages -l pt_BR
我还在 LANGUAGE_CODE = 'en-us'
中将 en-us 重命名为 en
请按照以下步骤操作。
1) 在根目录中创建文件夹 'locale'
。
2) 添加中间件
.....
'django.middleware.common.CommonMiddleware',
'django.middleware.locale.LocaleMiddleware',
....
3) 在settings.py中设置
USE_L10N = True
LANGUAGE_CODE = 'pt-BR'
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
LANGUAGES = (
('pt_BR', 'Portuguese'),
)
4) 运行 遵循命令
$ django-admin makemessages -l pt_BR -i env
$ django-admin compilemessages -l pt_BR
5) 然后在 locale > pt_BR > LC_MESSAGES > django.po
中更新所需的翻译文本,然后再次 运行
$ django-admin compilemessages -l pt_BR
我创建了一个简单的网站来测试国际化,但我无法让它按我想要的方式工作。我想更改 views.py 中的消息而不检查 request.LANGUAGE_CODE(显示正确)。
我可以访问前缀为 /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-admin 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'),
)
我认为我的路径不正确。我相信额外的斜线是错误的...我从 LOCALE_PATH 中删除了 /translations/ 并且它现在可以工作了。
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale'),
]
那我运行
django-admin compilemessages -l pt_BR
修改生成的*.po和运行
django-admin compilemessages -l pt_BR
我还在 LANGUAGE_CODE = 'en-us'
请按照以下步骤操作。
1) 在根目录中创建文件夹 'locale'
。
2) 添加中间件
.....
'django.middleware.common.CommonMiddleware',
'django.middleware.locale.LocaleMiddleware',
....
3) 在settings.py中设置
USE_L10N = True
LANGUAGE_CODE = 'pt-BR'
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
LANGUAGES = (
('pt_BR', 'Portuguese'),
)
4) 运行 遵循命令
$ django-admin makemessages -l pt_BR -i env
$ django-admin compilemessages -l pt_BR
5) 然后在 locale > pt_BR > LC_MESSAGES > django.po
中更新所需的翻译文本,然后再次 运行
$ django-admin compilemessages -l pt_BR