简体   繁体   English

django-admin.py makemessages 不工作

[英]django-admin.py makemessages not working

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?对于奖励投票,一个相关的问题:我安装它时没有链接 gettext ...对这个有什么帮助吗? Should I force it?我应该强迫它吗?

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.从那以后,我将翻译的名称更改为语言环境,并相应地更新了我的 settings.py。 then I ran this again and it's still complaining about gettext:然后我再次运行它,它仍然在抱怨 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 理解 homebrew 和 keg-only 依赖

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.

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 locale directory 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

Please try this in Ubuntu请在 Ubuntu 中试试这个

sudo apt-get install gettext

And use brew install gettext in OSX并在 OSX 中使用brew install gettext

Also make sure to set the local path in settings.py file.还要确保在 settings.py 文件中设置本地路径。

Here is the solution for those having problems with translations or are creating a multi-language site for the very first time in Django.这是为那些在翻译方面遇到问题或第一次在 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 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 Translations for more on this.有关更多信息,请参阅 翻译中的杂项。

Add the following imports, and encapsulate the urls you want translated with i18n_patterns.添加以下导入,并用 i18n_patterns 封装您要翻译的 url。 Here is what mine looks like:这是我的样子:

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.现在您可以进入您的模板并加载 i18n 模板标签,并在您要翻译的静态内容上使用 trans 和 transblock。 Here is an example:下面是一个例子:

{% 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.现在剩下的就是进入您的 /locales 文件夹,并编辑每个 .po 文件。 Fill in the data for each msgstr.为每个 msgstr 填写数据。 Here is one such example of that:这是一个这样的例子:

msgid "English"
msgstr "Angielski"

And finally compile the messages:最后编译消息:

./manage.py compilemessages

There is a lot more to learn with translations and internationalization is 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 .我还建议查看一些可用于 Django 的国际化包,如django-rosettadjango-linguo They help translate model content, django-rosetta does not create new entries for this in your database, while django-linguo does.它们帮助翻译模型内容,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!干杯!

For Mac users brew link gettext --force can be risk, as Brew advises.对于 Mac 用户brew link gettext --force可能有风险,正如 Brew 所建议的那样。 A better work around is to set a new PATH variable for your virtual environment.更好的解决方法是为您的虚拟环境设置一个新的PATH variable So, in the postactivate file, which is located in the bin folder of your virtual environment folder, type:因此,在位于虚拟环境文件夹的 bin 文件夹中的postactivate文件中,键入:

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

Note that you have to replace 0.19.7 by the version that is installed in your machine.请注意,您必须用机器中安装的版本替换0.19.7

And in your predeactivate file, which is located in the same folder of postactivate file, 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>使用python manage.py makemessages -l <desired_language> :) :)

Cheers.干杯。

对于macOS

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

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 命令

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的输出是什么?

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 PATH for the makemessages command.如果您不想链接gettext (您不应该这样做,因为弄乱 OS X 内部是不好的),那么您可以为makemessages命令设置PATH The following should work (but you need to adjust your gettext version number):以下应该有效(但您需要调整您的 gettext 版本号):

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

If you do it that way your installed gettext remains keg-only and django-admin will be happy and find all the programms it needs.如果你这样做,你安装的gettext仍然是 keg-only,django-admin 会很高兴找到它需要的所有程序。

One of the possibilities is that after you have successfully done all the above and done一种可能性是,在您成功完成上述所有操作并完成后

pip install python-gettext

you may have improperly configured your IDE or venv.您可能没有正确配置您的 IDE 或 venv。 In order to bypass this, go to the command prompt, navigate to your root folder and run py manage.py makemessages from there.为了绕过这个,进入命令提示符,导航到你的根文件夹并从那里运行py manage.py makemessages It will work.它会起作用。

For me, on Windows 10, the problem was that I used gettext installed with pip install (I got version 0.19-something).对我来说,在 Windows 10 上,问题是我使用了 gettext 安装 pip 安装(我得到的版本是 0.19-something)。 I made it work by:我通过以下方式使其工作:

  1. Uninstalling gettext with pip.使用 pip 卸载 gettext。
  2. downloading gettext (as a zip) from here istead: https://mlocati.github.io/articles/gettext-iconv-windows.html从这里下载 gettext(作为 zip): https://mlocati.github.io/articles/gettext-iconv-windows.html
  3. Extracting the files in a foler on "C:/program files"将文件提取到“C:/program files”文件夹中
  4. Added the path to the "bin" folder (inside the extracted folder) to PATH in my environment variables in windows.在我的环境变量 windows 中将“bin”文件夹(在提取的文件夹内)的路径添加到 PATH。

After that it worked.之后它起作用了。 (after 5 h frustrated debugging...) (经过 5 小时的沮丧调试...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM