0

I'm doing my first steps in Django and am trying to get translations of text to work which I'm passing into the application through an .ini file.

Say my init.ini is:

[test]
GREETING = Hello World

and in my settings.py, I'm doing:

from six.moves import configparser
from django.utils.translation import ugettext_lazy as _

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Retrieve initialization configuration
init_parser = configparser.ConfigParser()
init_parser.read(os.path.join(BASE_DIR, 'init.ini'))

...
HELLO = init_parser.get('test', 'GREETING')
HELLO = _(init_parser.get('test', 'GREETING'))

my tags translations do not show up when calling makemessage.

The documentation says

(The caveat with using variables or computed values, as in the previous two examples, is that Django’s translation-string-detecting utility, django-admin makemessages, won’t be able to find these strings. More on makemessages later.)

but they although makemessage is covered aplenty on the documentation page, it does not provide a solution on how to translate variables/computed values like this. Thus my question:

Is there any recommended practical way of passing strings as variables into a python module and have makemessage catch them?

EDIT:
adding the extension as in django-admin makemessages -l de --e=html,py,txt,ini doesn't work either, but now I'm curious how I could make a txt file that would be covered. Maybe that's an idea.

frequent
  • 27,643
  • 59
  • 181
  • 333

1 Answers1

0

So this django-admin makemessages -l de --e=html,py,txt,ini got me on the right track, because if txt is included by default as per the documentation, it must work somehow.

I created foo.txt:

_("yeah why not")
{% trans "maybe like this" %}

and the second snippet showed up in the .po file when calling makemessages.

I created foo.ini:

_("from ini, yeah why not")
{% trans "from ini, maybe like this" %}

and running django-admin makemessages -l de --e=html,py,txt,ini included the second snippet in the .po file as well.

So {% trans "" %}-ifying all text snippets in the initialisation file seems to do the job. I can then fetch the translated value from in my module.

frequent
  • 27,643
  • 59
  • 181
  • 333