我目前正在尝试设置我的网站进行翻译。 我的网站目前是法语,我想用英语翻译。这是我到目前为止所做的:
{% load i18n %}
标记放在html {% trans "Services" %}
我已将'django.middleware.locale.LocaleMiddleware'
添加到
中间件类列表。
我还将当前语言设置为
法语LANGUAGE_CODE = 'fr'
我已经指定了哪些语言可用
ugettext = lambda s: s
LANGUAGES = (
('en', ugettext('Anglais')),
('fr', ugettext('Français')),
)
我已使用USE_I18N = True
我已经指定了我希望本地化文件的位置
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale/'),
)
当我运行django-admin.py makemessages -l en -v3
时,这就是我得到的:
PS C:\Users\USERNAME\Desktop\Ajitech\ajitech-website> django-admin.py makemessages -l en -v3
examining files with the extensions: .txt and .html
ignoring directory .git
ignoring file settings.cpython-34.pyc in .\AjitechRoot\__pycache__
ignoring file urls.cpython-34.pyc in .\AjitechRoot\__pycache__
ignoring file wsgi.cpython-34.pyc in .\AjitechRoot\__pycache__
ignoring file __init__.cpython-34.pyc in .\AjitechRoot\__pycache__
ignoring file __init__.cpython-34.pyc in .\AjitechWebsite\migrations\__pycache__
ignoring file admin.cpython-34.pyc in .\AjitechWebsite\__pycache__
ignoring file models.cpython-34.pyc in .\AjitechWebsite\__pycache__
ignoring file views.cpython-34.pyc in .\AjitechWebsite\__pycache__
ignoring file __init__.cpython-34.pyc in .\AjitechWebsite\__pycache__
processing file AjitechData.sqlite3 in .
processing file __init__.py in .\AjitechRoot
processing file settings.py in .\AjitechRoot
processing file urls.py in .\AjitechRoot
processing file wsgi.py in .\AjitechRoot
processing file __init__.py in .\AjitechWebsite
processing file admin.py in .\AjitechWebsite
processing file __init__.py in .\AjitechWebsite\migrations
processing file models.py in .\AjitechWebsite
processing file tests.py in .\AjitechWebsite
processing file views.py in .\AjitechWebsite
processing file Runserver.ps1 in .
processing file db.sqlite3 in .
processing file manage.py in .
processing file base.css in .\static\static-only\admin\css
processing file changelists.css in .\static\static-only\admin\css
processing file dashboard.css in .\static\static-only\admin\css
processing file forms.css in .\static\static-only\admin\css
processing file ie.css in .\static\static-only\admin\css
processing file login.css in .\static\static-only\admin\css
processing file rtl.css in .\static\static-only\admin\css
processing file widgets.css in .\static\static-only\admin\css
processing file changelist-bg.gif in .\static\static-only\admin\img
processing file changelist-bg_rtl.gif in .\static\static-only\admin\img
processing file default-bg-reverse.gif in .\static\static-only\admin\img
processing file default-bg.gif in .\static\static-only\admin\img
processing file deleted-overlay.gif in .\static\static-only\admin\img
processing file move_vertex_off.png in .\static\static-only\admin\img\gis
processing file move_vertex_on.png in .\static\static-only\admin\img\gis
processing file icon-no.gif in .\static\static-only\admin\img
processing file icon-unknown.gif in .\static\static-only\admin\img
processing file icon-yes.gif in .\static\static-only\admin\img
processing file icon_addlink.gif in .\static\static-only\admin\img
processing file icon_alert.gif in .\static\static-only\admin\img
processing file icon_calendar.gif in .\static\static-only\admin\img
processing file icon_changelink.gif in .\static\static-only\admin\img
processing file icon_clock.gif in .\static\static-only\admin\img
processing file icon_deletelink.gif in .\static\static-only\admin\img
processing file icon_error.gif in .\static\static-only\admin\img
processing file icon_searchbox.png in .\static\static-only\admin\img
processing file icon_success.gif in .\static\static-only\admin\img
processing file inline-delete-8bit.png in .\static\static-only\admin\img
processing file inline-delete.png in .\static\static-only\admin\img
processing file inline-restore-8bit.png in .\static\static-only\admin\img
processing file inline-restore.png in .\static\static-only\admin\img
processing file inline-splitter-bg.gif in .\static\static-only\admin\img
processing file nav-bg-grabber.gif in .\static\static-only\admin\img
processing file nav-bg-reverse.gif in .\static\static-only\admin\img
processing file nav-bg-selected.gif in .\static\static-only\admin\img
processing file nav-bg.gif in .\static\static-only\admin\img
processing file selector-icons.gif in .\static\static-only\admin\img
processing file selector-search.gif in .\static\static-only\admin\img
processing file sorting-icons.gif in .\static\static-only\admin\img
processing file tooltag-add.png in .\static\static-only\admin\img
processing file tooltag-arrowright.png in .\static\static-only\admin\img
processing file LICENSE-JQUERY.txt in .\static\static-only\admin\js
processing file SelectBox.js in .\static\static-only\admin\js
processing file SelectFilter2.js in .\static\static-only\admin\js
processing file actions.js in .\static\static-only\admin\js
processing file actions.min.js in .\static\static-only\admin\js
processing file DateTimeShortcuts.js in .\static\static-only\admin\js\admin
processing file RelatedObjectLookups.js in .\static\static-only\admin\js\admin
processing file calendar.js in .\static\static-only\admin\js
processing file collapse.js in .\static\static-only\admin\js
processing file collapse.min.js in .\static\static-only\admin\js
processing file core.js in .\static\static-only\admin\js
processing file inlines.js in .\static\static-only\admin\js
processing file inlines.min.js in .\static\static-only\admin\js
processing file jquery.init.js in .\static\static-only\admin\js
processing file jquery.js in .\static\static-only\admin\js
processing file jquery.min.js in .\static\static-only\admin\js
processing file prepopulate.js in .\static\static-only\admin\js
processing file prepopulate.min.js in .\static\static-only\admin\js
processing file timeparse.js in .\static\static-only\admin\js
processing file urlify.js in .\static\static-only\admin\js
processing file animation.css in .\static\static\css
processing file bootstrap.min.css in .\static\static\css
processing file font-awesome.min.css in .\static\static\css
processing file FontAwesome.otf in .\static\static\fonts
processing file fontawesome-webfont.eot in .\static\static\fonts
processing file fontawesome-webfont.svg in .\static\static\fonts
processing file fontawesome-webfont.ttf in .\static\static\fonts
processing file fontawesome-webfont.woff in .\static\static\fonts
processing file fontawesome-webfont.woff2 in .\static\static\fonts
processing file ajitech-jumbotron-background-color-small.png in .\static\static\images
processing file ajitech-jumbotron-background-small.png in .\static\static\images
processing file ajitech-jumbotron-background.png in .\static\static\images
processing file ajitech-jumbotron-even.png in .\static\static\images
processing file ajitech-jumbotron-odd.png in .\static\static\images
processing file ajitech-logo-large.png in .\static\static\images
processing file ajitech-logo.png in .\static\static\images
processing file ajitech-logo.svg in .\static\static\images
processing file programming.jpg in .\static\static\images\services
processing file username-normal.png in .\static\static\images\team
processing file ajitech.js in .\static\static\js
processing file bootstrap.min.js in .\static\static\js
processing file jquery-2.1.3.min.js in .\static\static\js
processing file less-2.5.0.min.js in .\static\static\js
processing file wow.min.js in .\static\static\js
processing file ajitech-404.less in .\static\static\less
processing file ajitech-utilities.less in .\static\static\less
processing file ajitech.less in .\static\static\less
processing file 404.html in .\static\templates
processing file overview.html in .\static\templates
processing locale en
我要翻译的邮件位于overview.html中,已由脚本处理(最后处理的文件)。
毕竟,我希望在locale文件夹中生成一个.po文件,其中包含要转换为英文的“Service”行。但事实并非如此。有什么我想念的吗?
这是我的settings.py
"""
Django settings for AjitechRoot project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '** REDACTED **'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ALLOWED_HOSTS = [ '127.0.0.1' ]
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'AjitechWebsite',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.i18n',
)
ROOT_URLCONF = 'AjitechRoot.urls'
WSGI_APPLICATION = 'AjitechRoot.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'AjitechData.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'fr'
TIME_ZONE = 'Eastern/Canada'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale/'),
)
ugettext = lambda s: s
LANGUAGES = (
('en', ugettext('Anglais')),
('fr', ugettext('Français')),
)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
# Template location
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'static/templates'),
)
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/static-only')
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/static'),
)
编辑:
我正在使用Django 1.8.1。
makemessages命令从我的网站的根文件夹中调用,您可以在其中找到manage.py。
答案 0 :(得分:1)
根据documentation,LocaleMiddleware类必须位于SessionMiddleware之后和CommonMiddleware之前的变量MIDDLEWARE_CLASSES中。例如:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
)
此外,至少在Django 1.8中,我必须手动创建我希望翻译的每个应用程序中的语言环境目录。
您还应该在settings.py文件中使用函数ugettext_lazy():
from django.utils.translation import ugettext_lazy as _
LANGUAGES = (
('de', _('German')),
('en', _('English')),
)
希望这有帮助。
修改强>
django.core.context_processors中的内置模板上下文处理器were moved到Django 1.8中的django.template.context_processors,所以你应该使用:
TEMPLATE_CONTEXT_PROCESSORS = (
'django.template.context_processors.i18n',
)
但是,没有必要覆盖default settings,因为默认情况下包含了i18n上下文处理器。
考虑到自Django 1.8以来TEMPLATE_CONTEXT_PROCESSORS为deprecated。
答案 1 :(得分:1)
事实证明,我正在运行一个弃用的GNU GetText版本(准确地说是0.12),而makemessages需要0.15或更高版本,如documentation中所述。我已经更新了工具,一切正常。