139

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?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Antoine M.
  • 3,631
  • 3
  • 15
  • 20

6 Answers6

187

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.

andyw
  • 3,505
  • 2
  • 30
  • 44
Antoine M.
  • 3,631
  • 3
  • 15
  • 20
  • Thanks, helped me. Just execute the `makemessages` command from that `locale` directory if it's already available. – SaeX Nov 11 '15 at 15:33
  • 6
    In Django 1.9 you need to define `LOCALE_PATHS` even if it is `locale` otherwise the compiled text won't be discoverable. – Wtower Dec 14 '15 at 14:07
  • 1
    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) – tobiasBora May 14 '19 at 15:18
  • 1
    Here is more info on how Django discovers that directory - https://docs.djangoproject.com/en/3.2/topics/i18n/translation/#how-django-discovers-translations – joshlsullivan Sep 03 '21 at 21:44
  • 1
    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](https://github.com/django/django/blob/4.1.3/django/core/management/commands/makemessages.py#L533-L535) them. If you want a project-wide `locale` dir, simply create it in the project dir. These are the simplest solutions. – x-yuri Dec 06 '22 at 06:41
  • 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... – Cerin Aug 24 '23 at 00:15
35

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
David Dehghan
  • 22,159
  • 10
  • 107
  • 95
  • 3
    you actually don't need to create a folder for each of the languages. django-admin makemessages will do this for you – MagicLAMP Nov 13 '15 at 05:38
  • 1
    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? – Vadorequest May 21 '18 at 11:44
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.

jnns
  • 5,148
  • 4
  • 47
  • 74
  • 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](https://github.com/django/django/blob/4.1.3/django/core/management/commands/makemessages.py#L533-L535) them. – x-yuri Dec 06 '22 at 06:31
  • 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. – x-yuri Dec 06 '22 at 06:43
  • I have LOCALE_PATHS specified, and I still get this error when run from my project root. – Cerin Aug 24 '23 at 00:18
  • @Cerin what about the folder? Did you create that as well? – jnns Aug 25 '23 at 05:47
  • @jnns 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. – Cerin Aug 26 '23 at 18:51
0

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.

x-yuri
  • 16,722
  • 15
  • 114
  • 161
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.

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

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129