How can I use line breaks in translation strings inside .po files?
Currently I added "\n" and used {{msg|linebreaks}} in my template, but the string is printed in one single line... how can I print it on multiple lines?
I assume you want HTML output. In such case, it would be better to include HTML tags in the string:
{% blocktrans %}
First line<br/>
Second line
{% endblocktrans %}
If you are looking for something else, please describe better what you are actually trying to achieve.
Update:
If the lines are independent, it might be even better to actually split them up:
{% trans "First line" %}
<br/>
{% trans "Second line" %}
bit late, but maybe helps someone, add your translation like this:
po file
msgid "_your_msgid"
msgstr "first line\nsecond line"
template
{% trans '_your_msgid' as local_var %}
{{ local_var|linebreaksbr }}
You can use
character both in django template file and your .po files
Like this .po:
msgid "Psycological
aid"
msgstr "Психологическая
помощь"
django template:
{% trans 'Psycological
aid' %}
Containing block should have white-space: pre-line; css rule
Related
i want in same line print two value look like "1. Question ...".
but first {{ }} after set new line. look like this,
"1."
"Question ..."
{% for q in question %}
<p> {{ forloop.counter }}. {{ q.question|safe }}</p>
{% endfor %}
How can i print two value in same line in template ?
I want this:
1.Question
2.Question
...
Based on your comment, you say that q.question is the content of a CKEditor. Often times, these output at least wrap the content inside a <p> tag. In this case, the result output generated by Django would a nested <p> tag inside the <p> from your template:
<p>1. <p>Question</p></p>
This is invalid HTML, but the browser tries to render it as best as it can. I think you can either include the number inside the CKEditor and exclude it from your template or change your field to store a simple CharField, and keep your HTML unchanged.
This depends on the flexibility you want in your application.
I use i18n in my Jinja2 templates and everything works fine for plain text.
But I can't find any info how to use translations with variables, which I can't avoid using. Here is an example:
{% set words = ["Hello", "world"] %}
{% for word in words %}
{{ _(word) }}
{% endfor %}
What should I do to get "Hello" and "world" in my .po file?
It turned out I needed to use Babel for jinja2:
http://jinja.pocoo.org/docs/2.10/integration/
Posting here, because someone can be in the same trouble
I have this block in my HTML
...
<a class="header" href="{% url 'listing' house_post.id %}">
{% blocktrans with house_type=house_post.house_type.name trimmed %}
{{house_type}}
{% endblocktrans %}
</a>
...
One value of house_type is "Condominium". I've added the following entry in my .po file.
msgid "Condominium"
msgstr "ኮንዶሚኒየም"
I've run compilemessages on the po file, and the rest of the translations work when I switch languages. And I've made sure the value of house_type is set to "Condominium". But for some reason it's not being translated.
In addition when I run makemessages the tool comments out additions I've made in the .po files. I'm uncommenting them before running compilemessages. I don't know why it's doing that though it might be a clue.
It is possible to add translation texts to .po files. Isn't it?
It's not translated because {{house_type}} will have the value of house_post.house_type.name.
The blocktrans actually does nothing in your code. You would need it if you want to add a translatable text to the sentence. Ex:
{% blocktrans with house_type=house_post.house_type.name trimmed %}
{{house_type}} Translate this part
{% endblocktrans %}
If you want to have a translated variable, you have to pass the translations to house_post.house_type.name.
The content of your blocktrans is most likely the content of {{house_type}}. Not sure where it comes from, but this is where you have to translate it. Don't forget to insert something like
from django.utils.translation import ugettext_lazy as _
to header of your py-files.
In Django templates, you can use either {{ _("Hello World") }} or {% trans "Hello World" %} to mark strings to be translated. In docs, the “official” approach seems to be the {% trans %} tag, but the _() syntax is mentioned too once.
How these approaches differ (except syntax) and why should be one preferable rather than the other?
One difference is that you obviously can't use {% trans %} with tags and filters. But does that mean that I can just use _() everywhere, like {{ _("String") }}? It works and looks much cleaner and more consistent than using {% trans "String" %} with standalone strings and _() with tags and filters.
So it seems that there's technically no difference as of Django 1.5. Template engine internally marks a variable for translation (by setting its translate attribute) in two cases:
when you do {% trans VAR %} (see TranslateNode), or
if the name of a variable starts with _( and ends with ) (see Variable.__init__).
Later, when the variable is being resolved, Django wraps it with ugettext or pgettext if it sees the translate attribute.
However, as can be seen from source code, there are some flexibility considerations in favor of {% trans %} tag:
you can do {% trans "String" noop %}, which will put the string for translation into .po files, but won't actually translate the output when rendering (no internal translate attribute on variable, no ugettext call);
you can specify message context*, like {% trans "May" context "verb" %};
you can put translated message into a variable for later use*, like {% trans "String" as
translated_string %}.
* As of Django 1.4.
Please feel free to correct me or post a better answer in case I'm missing anything.
The trans template tag calls the ugettext() function. In Django _() is an alias to ugettext().
This is covered in the django docs.
I have some static text that needs to show up at 2 locations within a template.
For example:
<div>
{% if something %}
This is a static text
{% else %}
Something else happened
{% endif %}
</div>
... more html
<span>
{% if something %}
This is a static text
{% else %}
Something else happend
{% endif %}
</span>
I can do the above by duplicating the above text at 2 different locations in my template file(as shown above).
I could also create a model which will store the text(This is DRY but cost a call to the DB for a simple task)
I'm thinking of using include template but that's probably not the best way to achieve my goal.
What's the best way to do it?
Definitely use Inclusion Tags:
http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags
The tag file would either be something super simple like just the text "This is a static text" or the entire block:
{% if something %}
This is a static text
{% else %}
Something else happened
{% endif %}
"something" can be passed as a variable to the template tag so you can use that entire block in a variable way.
I use the django internationalization to do that. So in my apps/template I just write the key, and in the .po files is the value of the keys.
{% load i18n %}
<div>
{% if something %}
{% trans "static" %}
{% else %}
{% trans "something else" %}
{% endif %}
</div>
And in my .po file:
msgid "static"
msgstr "This is a static text"
msgid "something else"
msgstr "Something else happened
Besides useful for multi-language, it's much easier for copy writing just in case you want to change it in the future because you can just look unto one file instead of browsing several templates.
There are several ways, but it probably depends on what the text is and how often it will be used. It's hard to recommend a specific choice without full details
Create a custom template tag (this one makes the most sense based on how you've described your problem above).
Create a base template which has the text in it at the correct location and then inherit off of it for your "2 locations"
Put the static piece of text in a settings file and pass it to the template renderer via Context (probably not the best idea, but depending on what you're doing it could be a possibility)
You could use flatblocks : http://github.com/zerok/django-flatblocks
or chunks : http://code.google.com/p/django-chunks/
Those may be overkill for your problem, since they store your snippets in the database, but they add the benefit of making it possible to edit them via the admin.
{% load chunks %}
<div>
{% if something %}
{% chunk "something" %}
{% else %}
{% chunk "something_else" %}
{% endif %}
</div>
There are lots of forks or similar projects, for example:
http://bitbucket.org/hakanw/django-better-chunks/
http://github.com/bartTC/django-generic-flatblocks
I have a file like Java properties that I use for all of my resource strings. I just serve up the one that I want. Keeping these in one place also makes translating easy.
Ex.:
welcome_msg="hello user!"
thank_you="thank you"
goodbye_msg="goodbye, " + thank_you
If the included text gets bigger, use an 'include' tag.
{% include "myapp/helptext.html" %}
GrtzG