1

在过去的一年里,我一直在使用 Django 开发一个应用程序。该应用程序是在 Debian 环境中编写的,开发和测试完全在 debain 机器中完成。

最近,我决定将应用程序移至本地基于 Mac OS X 的笔记本电脑上。使用端口我做了一个虚拟环境并从 github 拉我的项目。当我尝试运行服务器时,出现“不支持的语言环境设置”异常。

我的应用程序大量使用语言环境,我注意到虽然我机器中的语言设置设置为 en_US.UTF-8,而在 debian 机器上设置为 en_US.UTF8。我不知道破折号是否是它失败的原因。

我的应用程序使用 django-localeurl 为 i10n 和 i18n 创建语言代码 url 架构。

下面是我得到的例外

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/en/

Django Version: 1.3.1
Python Version: 2.7.1
Installed Applications:
['localeurl',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'django.contrib.comments',
 'libraries.plugins.piston',
 'nani',
 'haystack',
 'compressor',
 'libraries.plugins.social_auth',
 'registration',
 'djcelery',
 'django.contrib.markup',
 'south',
 'libraries.plugins.storages',
 'libraries.plugins.django_messages',
 'merlin',
 'seacucumber',
 'timezones',
 'treebeard',
 'apps.core',
 'apps.core.mediastore',
 'apps.core.accounts',
 'apps.core.mazvars',
 'apps.core.mazguard',
 'apps.core.system',
 'apps.core.sponsorship',
 'apps.portal.default',
 'apps.portal.support',
 'apps.portal.pages',
 'apps.portal.blog',
 'apps.portal.developer',
 'apps.listing',
 'apps.listing.postfix',
 'apps.listing.category',
 'apps.listing.post',
 'apps.listing.home',
 'apps.listing.shoutout',
 'apps.listing.taggables',
 'apps.listing.comment',
 'apps.listing.badge',
 'apps.listing.geo',
 'apps.listing.reputation',
 'apps.mzn.shorturl',
 'grappelli.dashboard',
 'grappelli',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'debug_toolbar']
Installed Middleware:
('localeurl.middleware.LocaleURLMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'apps.core.middleware.FallbackLocaleMiddleware',
 'apps.core.accounts.middleware.LoggingMiddleware',
 'apps.core.accounts.middleware.CompleteEmail',
 'apps.core.accounts.middleware.setref',
 'apps.core.accounts.middleware.FollowUser',
 'apps.core.accounts.middleware.UnFollowUser',
 'apps.core.accounts.middleware.FraudLocked',
 'apps.core.middleware.DefaultAdminLocaleMiddleware',
 'apps.core.middleware.DefaultSiteMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')


Traceback:
File "/Users/mo/Projects/pythonic/mazban/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/mo/Projects/pythonic/mazban/mazban/apps/listing/home/views.py" in home
  23.     cal = render_event_calendar_widget(language_code=request.LANGUAGE_CODE)
File "/Users/mo/Projects/pythonic/mazban/mazban/apps/listing/post/event_views.py" in render_event_calendar_widget
  115.     return cal.render()
File "/Users/mo/Projects/pythonic/mazban/mazban/apps/core/utils.py" in render
  81.             [day for day in cal.formatweekheader(self.wh).split(' ')]
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/calendar.py" in formatweekheader
  297.         return ' '.join(self.formatweekday(i, width) for i in self.iterweekdays())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/calendar.py" in <genexpr>
  297.         return ' '.join(self.formatweekday(i, width) for i in self.iterweekdays())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/calendar.py" in formatweekday
  511.         with TimeEncoding(self.locale) as encoding:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/calendar.py" in __enter__
  490.         _locale.setlocale(_locale.LC_TIME, self.locale)
File "/Users/mo/Projects/pythonic/mazban/lib/python2.7/locale.py" in setlocale
  531.     return _setlocale(category, locale)

Exception Type: Error at /
Exception Value: unsupported locale setting

有什么想法可能导致这种情况吗?

更新:

当我登录到终端并写下“语言环境”时,我得到了以下信息

(mazban)Mohammed-Mughrabis-MacBook-Pro:mazban mo$ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL=
4

2 回答 2

3

为了将来参考,我通过将 LC_ALL 导出为“en_US”解决了这个问题,这解决了我的问题

export LC_ALL='en_US'

于 2012-06-16T16:01:54.790 回答
3

您在代码中的哪个位置设置它?这对我有用,因为 Linux 和 OSX 机器各不相同,而且我在两者上都使用了我的代码,所以我写了这个异常块:

try:
    import locale
    locale.setlocale(locale.LC_ALL, 'en_US.utf8')
except Exception:
    try:
        locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
    except Exception as e:
        messages.error(request, 'An error occurred: {0}'.format(e))
于 2014-01-07T15:55:46.867 回答