Home > other >  How to provide translations for django standalone app inside app?
How to provide translations for django standalone app inside app?

Time:10-12

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.

CodePudding user response:

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.

CodePudding user response:

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
  • Related