Unable to find a locale path to store translations for file __init__.py
Asked Answered
C

8

140

I'm trying to translate a Django app. I created some strings with {% trans %} in my templates. However, when I execute the following command in my app folder, I receive an error message:

$ django-admin.py makemessages -l fr 
CommandError: Unable to find a locale path to store translations for file __init__.py`

What did I do wrong?

Coruscation answered 24/7, 2014 at 14:53 Comment(0)
C
190

Turns out you need to create a locale folder first using mkdir locale. If you are running the command from within an app folder, you need a locale folder within that app folder.

Coruscation answered 24/7, 2014 at 15:9 Comment(6)
Thanks, helped me. Just execute the makemessages command from that locale directory if it's already available.Proctology
In Django 1.9 you need to define LOCALE_PATHS even if it is locale otherwise the compiled text won't be discoverable.Forwardlooking
Note that this folder does not have any 's', and that it must be in the app folder (that way you don't have any LOCALE_PATHS to configure)Externalism
Here is more info on how Django discovers that directory - docs.djangoproject.com/en/3.2/topics/i18n/translation/…Neckcloth
You don't need to cd into an app dir to have per-app locale dirs. Just make sure all your apps with translation strings have a locale dir. Then makemessages will find them. If you want a project-wide locale dir, simply create it in the project dir. These are the simplest solutions.Warford
This makes no sense. The command says it scans all your code. I have "locale" folders in my apps, which it scans, but yet it still gives me this error...Derogatory
M
36

Actually you can configure where the locale folder is. In your settings.py add:

LOCALE_PATHS = (
    PROJECT_ROOT + '/website/locale', )

Then create a folder for each of the languages you want to translate:

mkdir -p website/locale/de
Mithgarthr answered 23/1, 2015 at 6:48 Comment(3)
you actually don't need to create a folder for each of the languages. django-admin makemessages will do this for youSterne
This is confusing, are we supposed to create one entry for every app? Or store all locales for all apps in the same locale folder?Mossy
Since Django 4 this is LOCALE_PATHS = [ BASE_DIR / 'my_app' / 'locale' ]Sternutatory
S
20

The problem is that the command is not run from the app directory but from the project directory. This snippet from the docs explains it:

Turns out you need to create a locale folder first using mkdir locale.

./manage.py makemessages […] Runs over the entire source tree of the current directory and pulls out all strings marked for translation. It creates (or updates) a message file in the conf/locale (in the Django tree) or locale (for project and application) directory.

So, you either run the command from the app directory:

$ cd app
$ django-admin makemessages -l <locale>

… or you define a project wide locale directory using LOCALE_PATHS and you can run makemessages from the main directory from there on.

Either way, you should check that the ./locale/directory is present and create it using

$ mkdir locale

in case it's not.

Surra answered 11/1, 2017 at 11:30 Comment(5)
You don't need to cd into an app dir to have per-app locale dirs. Just make sure all your apps with translation strings have a locale dir. Then makemessages will find them.Warford
And the default project-wide locale dir is locale (in the project dir). In any case, you don't need to cd, just make sure your locale dirs exist.Warford
I have LOCALE_PATHS specified, and I still get this error when run from my project root.Derogatory
@Derogatory what about the folder? Did you create that as well?Surra
@Surra Yes. The solution was I had to set PYTHONPATH to include my local project directory and also set DJANGO_SETTINGS_MODULE to point to my settings module. Othewise, django-admin couldn't import my settings module, even though it was in my CWD.Derogatory
W
1

If you want per-app locale dirs, simply create them in every app dir with translation strings (that has files with translation strings) before running makemessages. And django will find them. No need to cd.

If you want one project-wide locale dir, create it in the project dir before running makemessages.

Warford answered 6/12, 2022 at 6:48 Comment(0)
A
1

You must create locale folder just under your django-project folder as shown below. *The folder name must be locale according to my experiments:

django-project
 |-core
 |  └-settings.py
 |-app1
 |  |-models.py
 |  └-admin.py
 |-app2
 |  |-models.py
 |  └-admin.py
 └-locale # Here

Then, you can create django.po in each locale/<...>/LC_MESSAGES/ with the command below. *The command below can create or update one or more django.po:

django-admin makemessages --locale=en --locale=fr --locale=ja

Or:

django-admin.py makemessages -l en -l fr -l ja

Then, django.po is created in each locale/<...>/LC_MESSAGES/ as shown below:

django-project
 |-core
 |  └-settings.py
 |-app1
 |  |-models.py
 |  └-admin.py
 |-app2
 |  |-models.py
 |  └-admin.py
 └-locale
    |-en
    |  └-LC_MESSAGES
    |     └-django.po # Here
    |-fr
    |  └-LC_MESSAGES
    |     └-django.po # Here
    └-ja
       └-LC_MESSAGES
          └-django.po # Here

And, you can update all django.po in locale folder with the command below. *With the command below, you can only update django.po but cannot create django.po:

django-admin makemessages --all

Or:

django-admin makemessages -a

And, you can compile django.po to django.mo in each locale/<...>/LC_MESSAGES/ with the command below:

django-admin compilemessages

Then, django.po is compiled to django.mo in each locale/<...>/LC_MESSAGES/ as shown below:

django-project
 |-core
 |  └-settings.py
 |-app1
 |  |-models.py
 |  └-admin.py
 |-app2
 |  |-models.py
 |  └-admin.py
 └-locale
    |-en
    |  └-LC_MESSAGES
    |     |-django.po
    |     └-django.mo # Here
    |-fr
    |  └-LC_MESSAGES
    |     |-django.po
    |     └-django.mo # Here
    └-ja
       └-LC_MESSAGES
          |-django.po
          └-django.mo # Here

In addition, even if you create locale folder just under core, app1 and app2 folders as shown below:

django-project
 |-core
 |  |-settings.py
 |  └-locale # Here
 |-app1
 |  |-models.py
 |  |-admin.py
 |  └-locale # Here
 └-app2
    |-models.py
    |-admin.py
    └-locale # Here

Then, run the command below:

django-admin.py makemessages -l en -l fr -l ja

Then, you will still get the error below according to my experiments and opposed to How Django discovers translations so you must create locale folder just under your django-project folder:

CommandError: Unable to find a locale path to store translations for file manage.py. Make sure the 'locale' directory exists in an app or LOCALE_PATHS setting is set.

Apparel answered 16/5, 2023 at 9:8 Comment(1)
Thanks, after 4h of research, you are absolutely right. Presence of "locale" folder in the apps or in the main dir makes everything. LOCALE_PATHS is just needed for django to find the .mo fileGorrono
P
0

For me, I had LOCALE_PATHS set correctly, but I did not have the environment variables set. When I set the environment variables, I ran python manage.py makemessages -l de and it ran correctly.

Pylle answered 26/1, 2023 at 15:30 Comment(0)
S
0

Folks, one thing I noticed. I had to create the locale directory from Terminal within the app folder by issuing the command mkdir locale. While creating it from inside PyCharm I kept getting the error message:

CommandError: Unable to find a locale path to store translations for file allauth_introducao/init.py. Make sure the 'locale' directory exists in an app or LOCALE_PATHS setting is set.

Study answered 10/9, 2023 at 8:48 Comment(0)
A
0

Recently I had the same issue even though I have added LOCALE_PATHS to my project settings:

LOCALE_PATHS = (
    PROJECT_ROOT + '/website/locale', )

I solved this issue by using the following method:

cd website
django-admin makemessages -l ar
Afrikaner answered 25/3 at 4:58 Comment(1)
This is a duplicate of jnns's answer from over 7 years ago.Institutive

© 2022 - 2024 — McMap. All rights reserved.