Python django-admin.py makemessages 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23353113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:48:21  来源:igfitidea点击:

django-admin.py makemessages not working

pythondjangohomebrewgettext

提问by broinjc

I am trying to translate a string.

我正在尝试翻译一个字符串。

{% load i18n %}
{% trans "Well, Hello there, how are you?" %}

to...

到...

Hola amigo, ?que tal?

My settings.py file has this:

我的 settings.py 文件有这个:

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'translations'),
)

And I am getting this:

我得到了这个:

(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.

I also don't understand this error message.

我也不明白这个错误信息。

(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.

The docs: https://docs.djangoproject.com/en/1.6/ref/django-admin/#django-admin-makemessages

文档:https: //docs.djangoproject.com/en/1.6/ref/django-admin/#django-admin-makemessages

And for bonus upvotes, a related question: gettext wasn't linked when I installed it... Any help with this one? Should I force it?

对于额外的投票,一个相关的问题是:当我安装 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.

Thanks!

谢谢!



UPDATES:

更新:

I have since changed the name of translations to locale and updated my settings.py accordingly. then I ran this again and it's still complaining about gettext:

从那以后,我将翻译名称更改为语言环境并相应地更新了我的 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.

I also found this:

我也发现了这个:

Understand homebrew and keg-only dependencies

了解自制和仅桶的依赖关系

after reading this:

阅读本文后:

(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.

采纳答案by broinjc

After making sure I had this in settings:

在确保我在设置中有这个之后:

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)
print(LOCALE_PATHS)

I double checked I had the localedirectory in the right place with its name spelled correctly.

我仔细检查了locale目录是否在正确的位置,并且其名称拼写正确。

I ended up linking gettext (after asking about that on superuser):

我最终链接了 gettext (在询问超级用户之后):

brew link gettext --force

manage.py compilemessages

django-admin.py makemessages -l es

And BAM. I've got my po file.

和巴姆。我有我的 po 文件。

But the doctor says:

但是医生说:

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

回答by Gregg_1987

Have you added {% load i18n %}to the top of your template?

您是否已添加{% load i18n %}到模板的顶部?

Bonus: You don't need to link gettext, what is the output from brew doctor?

奖励:您不需要链接 gettext,输出是brew doctor什么?

回答by Vijesh Venugopal

Please try this in Ubuntu

请在 Ubuntu 中试试这个

sudo apt-get install gettext

sudo apt-get install gettext

And use brew install gettextin OSX

brew install gettext在 OSX 中使用

Also make sure to set the local path in settings.py file.

还要确保在 settings.py 文件中设置本地路径。

回答by Abin Abraham

Please install gettext in your ubuntu OS using sudo apt-get command

请使用 sudo apt-get 命令在您的 ubuntu 操作系统中安装 gettext

Or in Mac

或者在 Mac 中

using brew command

使用 brew 命令

回答by radtek

Here is the solution for those having problems with translations or are creating a multi-language site for the very first time in Django. Here is the way I do it, and I have been doing since Django 1.4, below is tested in 1.7.1:

这是为那些在翻译方面遇到问题或第一次在 Django 中创建多语言站点的人提供的解决方案。这是我做的方式,我从 Django 1.4 开始就一直在做,下面是在 1.7.1 中测试的:

In settings.py …

在 settings.py ...

Add to MIDDLEWEAR_CLASSES, locale, it enables language selection based on request:

添加到 MIDDLEWEAR_CLASSES,locale,它可以根据请求启用语言选择:

'django.middleware.locale.LocaleMiddleware',

Add LOCALE_PATHS, this is where your translation files will be stored, also enable i18N:

添加 LOCALE_PATHS,这是您的翻译文件将被存储的地方,同时启用 i18N:

USE_I18N = True

LOCALE_PATHS = (
    os.path.join(PROJECT_PATH, 'locale/'),
)

Set LANGUAGES that you will be translating the site to:

设置您要将网站翻译成的语言:

ugettext = lambda s: s
LANGUAGES = (
    ('en', ugettext('English')),
    ('fr', ugettext('French')),
    ('pl', ugettext('Polish')),
)

Add i18n template context processor, requests will now include LANGUAGES and LANGUAGE_CODE:

添加 i18n 模板上下文处理器,请求现在将包含 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',  
)

Nest, in urls.py :

嵌套,在 urls.py 中:

In url_patterns, add the below, it will enable the set language redirect view:

在 url_patterns 中,添加以下内容,它将启用设置语言重定向视图:

url(r'^i18n/', include('django.conf.urls.i18n')),

See Miscellaneous in Translationsfor more on this.

有关更多信息,请参阅翻译中的杂项。

Add the following imports, and encapsulate the urls you want translated with i18n_patterns. Here is what mine looks like:

添加以下导入,并用 i18n_patterns 封装您要翻译的 url。这是我的样子:

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')),
)

Note: You can also drop your admin urls into the i18n_patterns.

注意:您也可以将您的管理 url 放入 i18n_patterns。

Now anywhere you use text and want to convert it, import lazytext and wrap every string with it like so _('text'), you can even go to your other urls.py files and do url translation like so:

现在在任何你使用文本并想要转换它的地方,导入lazytext 并像这样_('text') 用它包装每个字符串,你甚至可以转到你的其他 urls.py 文件并像这样进行 url 翻译:

url(_(r'^dual_language/$'), landing, name='duallang_landing'),

You can wrap text that you want translated in your other files, such as models.py, views.py etc.. Here is an example model field with translations for label and help_text:

您可以将要翻译的文本包装在其他文件中,例如 models.py、views.py 等。这是一个示例模型字段,其中包含 label 和 help_text 的翻译:

name = models.CharField(_('name'), max_length=255, unique=True, help_text=_("Name of the FAQ Topic"))

Django translation docs are great for this!

Django 翻译文档非常适合这个!

In your html templates...

在您的 html 模板中...

Now you can go into your templates and load the i18n templatetag and use trans and transblock on the static stuff you want to translate. Here is an example:

现在您可以进入您的模板并加载 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 %}

Now run a makemessages for each of your locales:

现在为您的每个语言环境运行 makemessages:

./manage.py makemessages -l pl

And now all is left is to go into your /locales folder, and edit each of the .po files. Fill in the data for each msgstr. Here is one such example of that:

现在剩下的就是进入您的 /locales 文件夹,并编辑每个 .po 文件。为每个 msgstr 填写数据。这是一个这样的例子:

msgid "English"
msgstr "Angielski"

And finally compile the messages:

最后编译消息:

./manage.py compilemessages

There is a lot more to learn with translations and internationalizationis closely related to this topic, so check out the docs for it too. I also recommend checking out some of the internationalization packages available for Django like django-rosetta, and django-linguo. They help translate model content, django-rosetta does not create new entries for this in your database, while django-linguo does.

翻译还有很多东西需要学习,国际化与这个主题密切相关,所以也请查看相关文档。我还建议查看一些可用于 Django 的国际化包,如django-rosettadjango-linguo。它们帮助翻译模型内容,django-rosetta 不会在您的数据库中为此创建新条目,而 django-linguo 会。

If you followed this you should be off to a good start. I believe this is the most standardized way to get your site running in multiple languages. Cheers!

如果你遵循这个,你应该有一个好的开始。我相信这是让您的网站以多种语言运行的最标准化的方式。干杯!

回答by jschrewe

If you don't want to link gettext(which you shouldn't because messing about with OS X internals is bad) then you can set the PATHfor the makemessagescommand. The following should work (but you need to adjust your gettext version number):

如果你不想链接gettext(你不应该因为摆弄OS X的内部是坏的),那么你可以设置PATHmakemessages命令。以下应该有效(但您需要调整您的 gettext 版本号):

PATH=/usr/local/Cellar/gettext/<installed version>/bin/:$PATH && \
django-admin makemessages -l <language>

If you do it that way your installed gettextremains keg-only and django-admin will be happy and find all the programms it needs.

如果你这样做,你安装的gettext仍然是 keg-only,django-admin 会很高兴找到它需要的所有程序。

回答by reinaldoluckman

For Mac users brew link gettext --forcecan be risk, as Brew advises. A better work around is to set a new PATH variablefor your virtual environment. So, in the postactivatefile, which is located in the bin folder of your virtual environment folder, type:

brew link gettext --force正如 Brew 所建议的那样,Mac 用户可能会面临风险。更好的解决方法是PATH variable为您的虚拟环境设置一个新的。因此,在postactivate位于虚拟环境文件夹的 bin 文件夹中的文件中,键入:

export TEMP_PATH=$PATH
export PATH=$PATH:/usr/local/Cellar/gettext/0.19.7/bin

Note that you have to replace 0.19.7by the version that is installed in your machine.

请注意,您必须替换0.19.7为您机器中安装的版本。

And in your predeactivatefile, which is located in the same folder of postactivatefile, type:

predeactivate位于同一文件postactivate文件夹中的文件中,键入:

export PATH=$TEMP_PATH
unset TEMP_PATH

Now you can use the python manage.py makemessages -l <desired_language>without worries. :)

现在您可以python manage.py makemessages -l <desired_language>放心使用。:)

Cheers.

干杯。

回答by valex

For macOS:

对于macOS

brew install gettext export PATH="/usr/local/opt/gettext/bin:$PATH"

brew install gettext export PATH="/usr/local/opt/gettext/bin:$PATH"