django-admin.py makemessages 无法工作
我正在尝试翻译一个字符串。
{% load i18n %}
{% trans "Well, Hello there, how are you?" %}
到...
Hola amigo, ¿que tal?
我的settings.py文件里有这个:
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'translations'),
)
而我得到的是这个:
(env)glitch:translations nathann$ django-admin.py compilemessages
CommandError: Can't find msgfmt. Make sure you have GNU gettext tools 0.15 or newer installed.
我也不太明白这个错误信息。
(env)glitch:ipals nathann$ django-admin.py makemessages -l es
CommandError:
This script should be run from the Django Git tree or your project or
app tree. If you did indeed run it from the Git checkout or your project
or application, maybe you are just missing the conf / locale(in the
django tree) or locale(for project and application) directory? It is not
created automatically, you have to create it by hand if you want to
enable i18n for your project or application.
文档链接: https://docs.djangoproject.com/en/1.6/ref/django-admin/#django-admin-makemessages
另外还有一个相关的问题,作为额外的奖励:我安装gettext的时候没有链接... 有人能帮我解决这个吗?我应该强制链接吗?
glitch:translations nathann$ brew link gettext
Warning: gettext is keg-only and must be linked with --force
Note that doing so can interfere with building software.
谢谢!
更新:
我已经把translations的名字改成locale,并相应地更新了我的settings.py。然后我又运行了一次,结果还是在抱怨gettext:
(env)glitch:ipals nathann$ mv translations/ locale
(env)glitch:ipals nathann$ django-admin.py makemessages -l es
CommandError: Can't find xgettext. Make sure you have GNU gettext tools 0.15 or newer installed.
我还发现了这个:
在阅读完这个之后:
(env)glitch:ipals nathann$ brew install gettext
Warning: gettext-0.18.3.2 already installed
(env)glitch:ipals nathann$ brew link gettext
Warning: gettext is keg-only and must be linked with --force
Note that doing so can interfere with building software.
11 个回答
对于 macOS
系统:
你需要先安装一个叫做 gettext
的工具,可以用这个命令来安装:
brew install gettext
安装完成后,还需要把这个工具的路径添加到你的系统环境中,这样你才能在任何地方使用它。可以用下面的命令来设置:
export PATH="/usr/local/opt/gettext/bin:$PATH"
对于Mac用户来说,使用 brew link gettext --force
可能会有风险,因为Brew这样建议。一个更好的解决办法是为你的虚拟环境设置一个新的 PATH变量
。所以,在你的虚拟环境文件夹中的bin文件夹里的 postactivate
文件中,输入:
export TEMP_PATH=$PATH
export PATH=$PATH:/usr/local/Cellar/gettext/0.19.7/bin
注意,你需要把 0.19.7
替换成你电脑上安装的版本。
然后在同一个文件夹里的 predeactivate
文件中,输入:
export PATH=$TEMP_PATH
unset TEMP_PATH
现在你可以放心地使用 python manage.py makemessages -l <desired_language>
了。:)
祝好。
这里是给那些在Django中遇到翻译问题或者第一次创建多语言网站的人提供的解决方案。我一直以来都是这样做的,从Django 1.4开始,下面的内容在1.7.1中经过测试:
在settings.py文件中……
在MIDDLEWARE_CLASSES中添加locale,这样可以根据请求来选择语言:
'django.middleware.locale.LocaleMiddleware',
添加LOCALE_PATHS,这里是你的翻译文件存放的地方,同时启用国际化(i18n):
USE_I18N = True
LOCALE_PATHS = (
os.path.join(PROJECT_PATH, 'locale/'),
)
设置你要翻译的网站语言:
ugettext = lambda s: s
LANGUAGES = (
('en', ugettext('English')),
('fr', ugettext('French')),
('pl', ugettext('Polish')),
)
添加国际化模板上下文处理器,这样请求中就会包含LANGUAGES和LANGUAGE_CODE:
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n', # this one
'django.core.context_processors.request',
'django.core.context_processors.static',
'django.contrib.messages.context_processors.messages',
)
接下来,在urls.py文件中:
在url_patterns中添加下面的内容,这样可以启用设置语言的重定向视图:
url(r'^i18n/', include('django.conf.urls.i18n')),
想了解更多,可以查看翻译中的杂项部分。
添加以下导入,并用i18n_patterns包裹你想翻译的urls。我的代码看起来是这样的:
from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import ugettext_lazy as _
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^i18n/', include('django.conf.urls.i18n')),
)
urlpatterns += i18n_patterns('',
(_(r'^dual-lang/'), include('duallang.urls')),
(r'^', include('home.urls')),
)
注意:你也可以把管理员的urls放入i18n_patterns中。
现在在你使用文本并想要转换的地方,导入lazytext,并用它包裹每个字符串,比如这样 _('text')。你甚至可以在其他的urls.py文件中进行url翻译,像这样:
url(_(r'^dual_language/$'), landing, name='duallang_landing'),
你可以在其他文件中包裹你想翻译的文本,比如models.py、views.py等。以下是一个带有标签和帮助文本翻译的模型字段示例:
name = models.CharField(_('name'), max_length=255, unique=True, help_text=_("Name of the FAQ Topic"))
Django的翻译文档对此非常有帮助!
在你的html模板中……
现在你可以进入你的模板,加载i18n模板标签,并在你想翻译的静态内容上使用trans和transblock。这里是一个示例:
{% load i18n %}
{% trans "This is a translation" %}<br><br>
{% blocktrans with book_t='book title'|title author_t='an author'|title %}
This is {{ book_t }} by {{ author_t }}. Block trans is powerful!
{% endblocktrans %}
现在为每个语言环境运行makemessages:
./manage.py makemessages -l pl
接下来,只需进入你的/locales文件夹,编辑每个.po文件。为每个msgstr填写数据。以下是一个示例:
msgid "English"
msgstr "Angielski"
最后,编译这些消息:
./manage.py compilemessages
关于翻译还有很多内容可以学习,国际化与这个主题密切相关,所以也可以查看相关文档。我还推荐一些可用于Django的国际化包,比如django-rosetta和django-linguo。它们帮助翻译模型内容,django-rosetta不会在你的数据库中创建新条目,而django-linguo会。
如果你按照这些步骤操作,你应该能有一个好的开始。我相信这是让你的网站支持多语言的最标准方法。祝好运!
请在Ubuntu系统中尝试这个
sudo apt-get install gettext
在OSX系统中可以使用 brew install gettext
另外,确保在settings.py文件中设置好本地路径。
在设置中确认我有这个:
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
print(LOCALE_PATHS)
我又仔细检查了一下,确保locale
文件夹在正确的位置,并且名字拼写正确。
最后我链接了gettext(在superuser上问过这个问题):
brew link gettext --force
manage.py compilemessages
django-admin.py makemessages -l es
然后,哇,我得到了我的po文件。
但是医生说:
Warning: Some keg-only formula are linked into the Cellar.
Linking a keg-only formula, such as gettext, into the cellar with
`brew link <formula>` will cause other formulae to detect them during
the `./configure` step. This may cause problems when compiling those
other formulae.
Binaries provided by keg-only formulae may override system binaries
with other strange results.
You may wish to `brew unlink` these brews:
gettext