Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate placeholder in Django form

Tags:

python

django

I'm having issues translating the placeholder text of a text input field. I have translated my .po file with a translation of the placeholder, and when I load my page all translations work fine, except this single placeholder. And yes I have compiled my translation file.

It seems like it doesn't understand that it should display the translated version. Maybe the form is created without the context of knowing what the current language is.

How to fix this? How to translate placeholders in form widgets in Django?

This is my forms.py file:

from django import forms
from django.utils.translation import ugettext as _

class InlineSearch(forms.Form):
    query = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control input-lg', 'placeholder':_('Search for a country, city or address here...')}), localize=True    )

This is my template .html file:

{% load i18n %}
<div class="inline-search">
    <div class="container">
        <form method="get" action="">
            <div class="input-group">
                {% for field in formset %}
                    {{field}}
                {% endfor %}
                <span class="input-group-btn">
                    <button type="submit" action="submit" class="btn btn-primary btn-lg"><i class="fa fa-search"></i> {% trans "Search" %}</button>
                </span>
            </div>
        </form>
    </div>
</div>

This is my .po file:

#: .\search\forms.py:5
msgid "Search for a country, city or address here..."
msgstr "Sök ett land, stad eller adress här..."
like image 620
Marcus Lind Avatar asked Mar 24 '15 05:03

Marcus Lind


People also ask

How do I translate text in Django?

Use the function django. utils. translation. gettext_noop() to mark a string as a translation string without translating it.

What is a placeholder in translation?

It is a character, word, or string of characters that temporarily takes the place of the final data. It may also indicate where a programmer needs to add specific code that they have not yet written. As a result, placeholders need to appear in the translated text even if they are moved around for the sake of syntax.

What is placeholder in Django?

Placeholders are an easy way to define sections in an HTML template that will be filled with content from the database when the page is rendered. This content is edited using django CMS's frontend editing mechanism, using Django template tags. fullwidth. html contains a single placeholder, {% placeholder "content" %} .

What is Gettext_lazy in Django?

gettext_lazy is a callable within the django. utils. translation module of the Django project.


1 Answers

Try to use ugettext_lazy:

from django.utils.translation import ugettext_lazy as _

instead of

from django.utils.translation import ugettext as _

from the documentation (see bolds):

These functions store a lazy reference to the string – not the actual translation. The translation itself will be done when the string is used in a string context, such as in template rendering.

This is essential when calls to these functions are located in code paths that are executed at module load time.

This is something that can easily happen when defining models, forms and model forms, because Django implements these such that their fields are actually class-level attributes. For that reason, make sure to use lazy translations in the following cases:

like image 87
mastazi Avatar answered Sep 20 '22 14:09

mastazi