Django makemessages errors Unknown encoding "utf8"
Asked Answered
I

6

29

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?

Inheritance answered 6/12, 2010 at 19:14 Comment(7)
Did you try doing what it suggested?Morphinism
Yeah, tried doing this xgettext --from-code=utf-8 but had no success because it ask to me the input file, but I can't imagine witch files are related to this =( thanksInheritance
When reading an error message, don't stop after the first few words...Morphinism
the init.py file is clear! No code inside it! =( sorry, but I'm new to python. thanksInheritance
Yes Ignacio, you are right, I was lost and find the solution just calming down and reading it =(, never thought that the Django code would be wrong. The solution was at the file init.py inside the views, that had utf8, i changed to utf-8 and solved. Best regard's!Inheritance
@RodrigoFerrari but it isn't a solution if the init.py is part of a 3rd party lib inside your virtualenv. There you wouldn't change anything. My experiences are: working under Windows10, Ubuntu 16.04 but not under Ubuntu 17.04... strange thing... =(Koppel
That was 2010... ten years later we moved on but start to see Unknown encoding "latin-1" 😂Sympathize
G
62

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

Glarus answered 5/1, 2018 at 15:5 Comment(8)
./manage.py makemessage -l fr -i my_env Worked for me, THANKS!Acetylene
Worked! Thanks!Subtract
worked for me even though it was 'makemessages' with an 'S'.Zoan
@LéoChazMaltrait Indeed, I made a mistake in the code I wrote, Davide Pizzolato corrected it (makemessage > makemessages). Thx to himGlarus
any help with COMPILEMESSAGES instead? i tried with exclude without luck, i want to exclude the whole virtual environment folderOdelet
@Odelet instead of excluding you can also move the environment folder somewhere else, that way django commands should never encounter it.Maidie
@Maidie executed the command from one level below in the end #54229586Odelet
worked for me as well, thx.Faradic
L
13

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.

Legendre answered 15/8, 2011 at 23:21 Comment(0)
R
9

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.

Rather answered 27/1, 2020 at 18:55 Comment(0)
A
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).

Annitaanniversary answered 7/5, 2011 at 4:1 Comment(0)
A
0

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>
Aloke answered 23/1, 2021 at 16:3 Comment(0)
A
0

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.

Adeleadelheid answered 12/7, 2023 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.