28👍
I’m only just getting started with Django internationalization, but I think you’re misunderstanding how the {% blocktrans %}
tag handles placeholders.
The point of blocktrans
is to allow the text around the placeholders to be translated. It won’t translate anything inside {{...}}
.
If you look at the generated .po
file, you’ll see that the following template code:
{% blocktrans %}This is my variable: {{variable}}{% endblocktrans %}
Will get converted into something like the following:
msgid:"This is my variable: %s"
I don’t think you can translate a variable within a blocktrans tag. You can probably do constant strings with {% blocktrans with _("string") as x %}{{x}}{% endblocktrans %}
but I can’t think why you’d want to.
You’ll have to do what you want in your view or model code I think.
- How to send a request to another server in a django view?
- What is the different between the get logger functions from celery.utils.log and logging?
- Docker compose could not open directory permisson denied
- Does Django natively support common table expressions?
- Django 'ascii' codec can't encode character
5👍
As Tom pointed out blocktrans will preserve what you put inside the with statement instead of translating it. What you need to do is use the with before the translation. In your example, it would look like this:
{% with cat_slug=cat.name|slugify %}
{% trans cat_slug %}
{% endwith %}
P.S. I know I’m answering a 6yr old question, but I’ve run across this exact situation a couple times now and haven’t seen a SO question/answer that handles it.
2👍
{% blocktrans with cat.name as cat_slug %}{{ cat_slug|capfirst }}{% endblocktrans %}
?
EDIT: you were right the doc says the filter as to be placed in the blocktrans
- Django 'ascii' codec can't encode character
- How to write unit tests for django-rest-framework api's?
- How do I tell Django to save my test database?
- How should multiple Django apps communicate with each other?