60đź‘Ť
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
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.
- [Django]-How to update manytomany field in Django?
- [Django]-How to check Django version
- [Django]-More than 1 foreign key
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.
- [Django]-Django Model() vs Model.objects.create()
- [Django]-How can I filter a date of a DateTimeField in Django?
- [Django]-Docker-compose with django could not translate host name "db" to address: Name or service not known
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).
- [Django]-Django: Invalid filter
- [Django]-Access Django model's fields using a string instead of dot syntax?
- [Django]-Virtualenv(python3.4), pip install mysqlclient error
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>
- [Django]-<Django object > is not JSON serializable
- [Django]-Django models – how to filter number of ForeignKey objects
- [Django]-Is there a way to filter a queryset in the django admin?
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.
- [Django]-How to get getting base_url in django template
- [Django]-Deploying Google Analytics With Django
- [Django]-What is the canonical way to find out if a Django model is saved to db?