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.

asked Jul 1, 2018 at 17:48

1 Answer 1

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.

answered Jul 1, 2018 at 18:27
Sign up to request clarification or add additional context in comments.

2 Comments

Sadly while translatable tags can be detected like this, they don't get translated automatically.
They do if you strip {% trans "" %} and translate by hand

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.