Django makemessages 错误未知编码“utf8”

发布于 2024-10-06 04:25:29 字数 586 浏览 4 评论 0原文

我安装了与 yum 分开的 python。

现在,我需要重新编译 OSQA 系统的语言包,但收到此消息:

Error: errors happened while running xgettext on __init__.py
xgettext: ./Django-1.2.3/tests/regressiontests/views/__init__.py:1: Unknown encoding "utf8". Proceeding with ASCII instead.
xgettext: Non-ASCII string at ./Django-1.2.3/tests/regressiontests/views/__init__.py:7.
          Please specify the source encoding through --from-code or through a comment
          as specified in http://www.python.org/peps/pep-0263.html.

我尝试在 manage.py 文件中将编码设置为 utf-8,但它不起作用。

有人可以帮我解决这个问题吗?

I installed python separated from yum.

Now, I need to recompile the language pack for the OSQA system, but get this message:

Error: errors happened while running xgettext on __init__.py
xgettext: ./Django-1.2.3/tests/regressiontests/views/__init__.py:1: Unknown encoding "utf8". Proceeding with ASCII instead.
xgettext: Non-ASCII string at ./Django-1.2.3/tests/regressiontests/views/__init__.py:7.
          Please specify the source encoding through --from-code or through a comment
          as specified in http://www.python.org/peps/pep-0263.html.

I tried to set encode at utf-8 in the manage.py file but it didn't work.

Can someone help me to solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

樱娆 2024-10-13 04:25:29

我知道这篇文章已经过时了,但我今天遇到了同样的问题,我花了几个小时才找出原因。也许人们会遇到同样的情况:

我的 virtualenv 位于我的 django 根目录中:

这是我的项目树:

DjangoDirectory:

  • my_env
  • Django_App1
  • Django_App2
  • ...
  • ...
  • Manage.py

当我启动命令时:

./manage.py makemessages -l fr 

我收到相同的错误

Error: errors happened while running xgettext on __init__.py
...

:事实上,我注意到 xgettext 查看了我的文件夹中的所有文件以及 my_env 中的文件。

所以我发现 -i 标志在 makemessages 过程中忽略文件或文件夹

所以现在,使用下面的命令就像一个魅力,我不再收到错误了。

./manage.py makemessages -l fr -i my_env

希望它会有所帮助

I know this post is outdated but I had the same issue today, and it took me hours to find out why. Maybe people will be in the same case :

My virtualenv is in my django root directory :

Here is my project tree :

DjangoDirectory:

  • my_env
  • Django_App1
  • Django_App2
  • ...
  • ...
  • manage.py

When I launch command :

./manage.py makemessages -l fr 

I get the same error :

Error: errors happened while running xgettext on __init__.py
...

In fact, I noticed that xgettext looked into ALL the files in my folder, as well as files in my_env.

So I found the -i flag which ignore files or folders during the makemessages process

So now, with the command below it works like a charm and I don't get the error anymore.

./manage.py makemessages -l fr -i my_env

Hope it will help

菩提树下叶撕阳。 2024-10-13 04:25:29

实际上是的,我已经在 makemessages 中遇到了类似的问题,因为在每个源文件的顶部我写了“#coding: utf8”。尽管它可以与源编译一起使用,但我必须在每个文件中将“utf8”替换为“utf-8”。

如果您不习惯 makemessages,请注意应用于格式字符串的 gettext 函数,当存在多个占位符时,您将需要字符串来包含命名参数。
“%s”很好
“%(max)s”也不错
也有“%(min)s %(max)s”
“%s %s”不行。

Actually yes, I've already had similar problems with makemessages, because on top of every source file I wrote "# coding: utf8". Even though it worked with source compilation, I've had to replace "utf8" with "utf-8" in every file.

If you're not used to makemessages, take care of gettext functions applied to format strings, you will need strings to contain named parameters when there is more than one placeholder.
"%s" is good
"%(max)s" is good too
"%(min)s %(max)s" too
"%s %s" is not ok.

酷炫老祖宗 2024-10-13 04:25:29

实际上,如果您在 settings.py、视图和模板中正确完成了所有配置,并且安装了 gettext 并且一切正常,那么您可能需要检查您的虚拟环境在哪里。例如,如果它位于项目文件夹中的根文件夹内,我认为您的结构是 myapp_project/venv/

另外,我假设您在根 myapp_project 文件夹中创建了一个名为 locale 的空文件夹。

例如,如果您要翻译法语,请尝试这样运行:
注意:将 venv 替换为您命名的虚拟环境。

这是简短的答案

django-admin.py makemessages -l fr -i venv

上面的内容将为您提供 local/fr/LC_MESSAGES/django.po 但您现在必须运行第二个命令,将 makemessages 创建的 .po 文件编译为 LC_MESSAGES 中的 .mo 文件> 文件夹

所以,然后运行:

django-admin.py compilemessages

现在这应该会得到 django.po 文件。

这是很长的答案

如果您使用 i18n_patterns 正确配置了 urls.py,并且在 .po 中填写了 msgstr "" 的翻译文件,然后再次运行django-admin.pycompilemessages,您现在可以运行您的服务器并转到您想要的页面127.0.0.1:8000/fr/,您应该看到您的翻译。

如果您想知道您的 settings.py 文件应该是什么样子(法语)。至少一部分应该是这样的。

from django.utils.translation import gettext_lazy as _

LANGUAGES = [
   ('fr', _('French')),
   ('en', _('English')),
]

LANGUAGE_CODE = 'en-us'


TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

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

另外,请确保在 MIDDLEWARE 列表中的 settings.py 中添加该组件。 'django.middleware.locale.LocaleMiddleware' 当该组件从上到下执行时,将该组件放在列表中的哪个位置非常重要。所以把它放在sessions组件下。

对于您的主应用程序(您可能有几个或一个)urls.py 文件,请确保导入 from django.conf.urls.i18n import i18n_patterns

接下来,在同一个 urls.py 文件中,确保您实际添加/或编辑了 url 模式。以下是它的示例:

urlpatterns += i18n_patterns (
    path('', include('pages.urls')),
    path('meals/', include('meals.urls')),
    prefix_default_language=False
 ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

您可以使用它,显然它上面的路径不包括管理员。因此,只需尝试一下,只是让您了解它应该是什么样子才能使 /fr/ url 正常工作。

您还会注意到上面的 prefix_default_language=False 行是为了防止 /en/ 默认模式。在此示例中,我们将从英语翻译为法语。所以不需要在模式中包含 /en/ 。

不要忘记在您的模板中...是的,所有这些,无论它是否是父模板都没关系。您必须包含 {% load i18n %} 才能正常工作。

重要注意事项:
当您返回模板文件并开始更新文本内容并添加跨块等时。执行此操作后。返回到终端并停止服务器,如果您的根目录中有 venv 文件夹,则必须运行以下命令:

django-admin.py makemessages -l fr -i venv
django-admin.py compilemessages

现在您可以打开 django.po 文件并将翻译添加到模板中编辑的文本内容中。如果您的 venv 文件夹不在您的根文件夹中。您只需运行 django-admin.py makemessages -l fr 即可。

如果您不这样做,您最终将在 .po 文件中手动执行操作。你不想那样做。

我希望这有帮助。

Actually if you did all the configurations correctly in settings.py, views and templates and you installed gettext and all good, then you may want to check where your virtual environment is. For instance if it's inside your root folder in your project folder your structure I suppose is myapp_project/venv/

Also I'm assuming you've you created an empty folder called locale in your root myapp_project folder.

Try to run it like this if you're translating french for example:
and note: replace venv with whatever you named your virtual environment.

This is the short answer

django-admin.py makemessages -l fr -i venv

this above will get you the local/fr/LC_MESSAGES/django.po but you now have to run the second command to compile the .po file created by makemessages to a .mo file in the LC_MESSAGES folder

So, then run:

django-admin.py compilemessages

now this should get you the django.po file.

This is the long answer

If you configured your urls.py correctly with i18n_patterns, and you filled the translations of msgstr "" in your .po file and then run django-admin.py compilemessages again you can now run your server and go to your desired page 127.0.0.1:8000/fr/ and you should see your translations.

In case you are wondering what your settings.py file should look like (for french). It should look like this at least part of it.

from django.utils.translation import gettext_lazy as _

LANGUAGES = [
   ('fr', _('French')),
   ('en', _('English')),
]

LANGUAGE_CODE = 'en-us'


TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

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

Also in settings.py in the MIDDLEWARE list make sure you add the component. 'django.middleware.locale.LocaleMiddleware' and it's important where on the list you put that component as it executes from top to bottom. So put it under the sessions component.

For your main app (you may have a few or one) urls.py file make sure you import from django.conf.urls.i18n import i18n_patterns.

Next on the same urls.py file ensure that you actually add/or edit the url patterns alright. Here is an example of what it could look like:

urlpatterns += i18n_patterns (
    path('', include('pages.urls')),
    path('meals/', include('meals.urls')),
    prefix_default_language=False
 ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

You can play with this and obviously on the paths above it don't include the admin. So just play around but just giving you an idea of what it should look like in order for /fr/ url to work.

You will also notice above the line prefix_default_language=False this is to prevent the /en/ default pattern. In this example we are translating from English to French. So no need to have /en/ in the pattern.

Don't forget in your templates... yes all of them, it don't matter if it's parent template or not. You have to include {% load i18n %} for this to work.

Important to Note:
When you go back to your templates files and you start updating your text content and adding your transblocks etc. After you do this. Go back to your terminal and stop your server, and again if you have your venv folder in your root you must run these commands:

django-admin.py makemessages -l fr -i venv
django-admin.py compilemessages

Now you can open your django.po file and add your translations to the edited text content in your templates. If your venv folder ins't in your root folder. You can just run django-admin.py makemessages -l fr instead.

If you don't do this, you will end up doing things manually in your .po file. You don't wanna do that.

I hope this helped.

眸中客 2024-10-13 04:25:29

我已为此创建了一张票证 http://code.djangoproject.com/ticket/15980

这似乎是 Django 代码中的一个简单的拼写错误,问题是 python 将“utf8”视为“utf-8”的别名,但 xgettext 不这样做。从 SVN 中的 Django r16169 (05/06/11 12:49:06) 开始,该问题仍然存在。

编辑:该问题现已在 Django 源代码中得到修复(截至 2011 年 5 月)。

I've created a ticket for this at http://code.djangoproject.com/ticket/15980.

It appears to be a simple typo in the Django code, the problem being that python treats "utf8" as an alias for "utf-8", but xgettext does not. The problem still exists as of Django r16169 (05/06/11 12:49:06) in SVN.

EDIT: The issue has been fixed now in the Django source (as of May 2011).

━╋う一瞬間旳綻放 2024-10-13 04:25:29

最近我也遇到了同样的问题,网上也找不到合适的解决方案。经过几次尝试,我解决了我的案例中的错误。创建 trans 标记时,请确保对每个段落执行此操作(不包括段落分隔符)。

而不是:

<p>
 {% trans "
  Paragraph I

  Paragraph II
 " %}
</p>

这可能会解决错误:

<p>
 {% trans "Paragraph I" %}
</p>

<p>
 {% trans "Paragraph II" %}
</p>

Recently, I had the same issue, and I couldn't find a fitting solution online. After a few tries, I resolved the error in my case. While creating the trans tags, make sure you do this for each paragraph excluding the paragraph breaks.

Instead of:

<p>
 {% trans "
  Paragraph I

  Paragraph II
 " %}
</p>

This may solve the error:

<p>
 {% trans "Paragraph I" %}
</p>

<p>
 {% trans "Paragraph II" %}
</p>
花期渐远 2024-10-13 04:25:29

我得到了下面同样的错误:

xgettext: .\venv\Lib\site-packages\charset_normalizer\__init__.py:1: Unknown encoding "utf_8". Proceeding with ASCII instead.
xgettext: Non-ASCII string at .\venv\Lib\site-packages\charset_normalizer\__init__.py:12.
          Please specify the source encoding through --from-code or through a comment
          as specified in https://www.python.org/peps/pep-0263.html.

当我运行下面的命令时,该命令还搜索虚拟环境中的文件(在我的例子中是venv):

django-admin makemessages -l fr

发生错误的原因是因为虚拟环境中的某些文件具有错误的代码 utf8utf_8 而不是正确的代码 utf-8,因此要解决该错误,您需要替换 utf8某些文件中使用 code> 或 utf_8utf-8,但如果很多文件的代码错误,则需要很长时间才能完成此操作。 *您可以查看相关问题和答案

因此,最简单的方法是使用 --ignore venv-i venv 运行命令,忽略虚拟环境(在我的例子中为 venv):如下图,则错误已解决:

django-admin makemessages -l fr --ignore venv
django-admin makemessages -l fr -i venv

另外,如果运行以下命令:

django-admin makemessages --help
django-admin makemessages -h

然后,您可以看到 --ignore-i 的解释,如下所示:

--忽略模式,-i 模式

忽略与此 glob 样式模式匹配的文件或目录。使用多次可以忽略更多。

I got the same error below:

xgettext: .\venv\Lib\site-packages\charset_normalizer\__init__.py:1: Unknown encoding "utf_8". Proceeding with ASCII instead.
xgettext: Non-ASCII string at .\venv\Lib\site-packages\charset_normalizer\__init__.py:12.
          Please specify the source encoding through --from-code or through a comment
          as specified in https://www.python.org/peps/pep-0263.html.

When I ran the command below which also searches the files in virtual environment (venv in my case):

django-admin makemessages -l fr

The reason why the error occurs is because some files in virtual environment have the wrong code utf8 or utf_8 instead of the correct code utf-8 so to solve the error, you need to replace utf8 or utf_8 with utf-8 in some files but it takes much time to do that if a lot of files have the wrong code. *You can see the relative question and the answers.

So, the easiest way is to run the command with --ignore venv or -i venv ignoring virtual environment (venv in my case) as shown below, then the error is solved:

django-admin makemessages -l fr --ignore venv
django-admin makemessages -l fr -i venv

In addition, if you run these commands below:

django-admin makemessages --help
django-admin makemessages -h

Then, you can see the explanation of --ignore and -i as shown below:

--ignore PATTERN, -i PATTERN

Ignore files or directories matching this glob-style pattern. Use multiple times to ignore more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文