开发者

Django makemessages errors Unknown encoding "utf8"

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开发者_如何学Go file but it didn't work.

Can someone help me to solve this issue?


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


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.


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.


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


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜