0

I would want to provide translation texts (makemessages and write translation strings) inside django-standalone app, to make app support multiple languages. How it can be done?

Currently, I use from django.utils.translation import gettext to define translation strings. I would not want to run manage.py makemessages command in the parent project and repeat writing translation strings for each parent project.

Viljami
  • 639
  • 7
  • 15

2 Answers2

1

If you are using Linux you can use this code:

cd your_app_name
python ../manage.py makemessages

Also no difference in windows too but I never tested it!

For more information and understanding the priority of translation directories read this documentation.

mrash
  • 883
  • 7
  • 18
  • Thanks, the link to documentation helps me to understand translation discovery logic. I do not have parent project and manage.py file, since I develop my standalone app without parent project. For somebody else check my answer below to run `makemessages` without parent project – Viljami Oct 12 '21 at 09:03
  • Still, I think adding a parent project in general, would be a better solution, to make everything work in a normal way. – Viljami Oct 12 '21 at 09:12
  • The recommended solution is to use a simple parent project to testing code and using models. But you can use: https://stackoverflow.com/questions/16509160/ugettext-and-ugettext-lazy-functions-not-recognized-by-makemessages-in-python-dj – mrash Oct 12 '21 at 11:02
0

To run makemessages for app without a parent project I made the following python script makemessages.py, I put this inside my app root directory:

#!/usr/bin/env python

import sys
import django

from django.conf import settings
from django.core.management import call_command
from django.utils.translation import gettext_lazy as _

settings.configure(DEBUG=True,
    LANGUAGES = [
    ('fi', _('Suomi')),
    ('en', _('English')),
    ],
    USE_I18N = True,
    USE_L10N = True,
    USE_TZ = True,
    LOCALE_PATHS = (
        'locale/',
    )
)

django.setup()

call_command('makemessages', '-l', 'fi', '-l', 'en')

After that, I was able to create the first translations for the specified languages by running

python3 makemessages.py

After once ran, I was able to update translation strings and compile messages with

django-admin makemessages --all
django-admin compilemessages
Viljami
  • 639
  • 7
  • 15