我正在尝试使用django国际化框架在我的项目上激活法语。
这是我的urls.py
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
urlpatterns = i18n_patterns('',
url('$^', 'test_inter.views.test')
)
这是我的views.py
from django.utils.translation import ugettext, get_language
from django.http import HttpResponse
def test(request):
return HttpResponse('%s - %s' % (get_language(), ugettext('test_en')))
这是我的settings.py
# Django settings for test_inter project.
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'sass01aud', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
}
}
TIME_ZONE = 'America/Chicago'
LOCALE_PATHS = (
'/Users/osasseville/Documents/workspace/test_inter/src/locale'
)
def gettext(s): s
LANGUAGE_CODE = 'en'
LANGUAGES = (
('en', gettext('English')),
('fr', gettext('French')),
)
USE_I18N = True
USE_L10N = True # not required for this demo but important
SITE_ID = 1
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# Make this unique, and don't share it with anybody.
SECRET_KEY = '%!q$#o%9wta*m31c_nhp(x777_-^@&ebu%6da&^=a6t)nt(ux)'
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.locale.LocaleMiddleware',
)
ROOT_URLCONF = 'test_inter.urls'
WSGI_APPLICATION = 'test_inter.wsgi.application'
TEMPLATE_DIRS = (
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'test_inter',
)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
我创建了 locale 文件夹并运行django-admin
osasseville:src osasseville$ pwd
/Users/osasseville/Documents/workspace/test_inter/src
osasseville:src osasseville$ mkdir locale
osasseville:src osasseville$ django-admin.py makemessages -l fr
processing language fr
我编辑了我的语言环境/ fr / LC_MESSAGES / django.po
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-02-19 18:23-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1)\n"
#: test_inter/settings.py:31
msgid "English"
msgstr "Anglais"
#: test_inter/settings.py:32
msgid "French"
msgstr "Francais"
#: test_inter/views.py:5
msgid "test_en"
msgstr "test_fr"
最后,我编写了我的消息。
osasseville:src osasseville$ python manage.py compilemessages -l fr
processing file django.po in /Users/osasseville/Documents/workspace/test_inter/src/locale/fr/LC_MESSAGES
当我运行我的开发服务器时,这就是结果:
/ fr /给我fr - test_en / en /给我en - test_en
如您所见,我的语言正在切换,但无法在我的消息文件中显示test_fr。