Search code examples
djangointernationalizationtranslationdjango-i18n

Django translate input field placeholder


I have a placeholder in the search bar form. I want to translate it to the user's selected language. I tried to use the {% translate 'message to translate' %} but only get the first word shown in the bar (maybe a problem with the quoatation marks?).

The html file:

  <form class="form_search">
    {% csrf_token %}
    <div class="form-group input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-search" style="color:black"></i></span>
        <input type="search" class="form-control home_search rounded-end" placeholder= {% translate "placeholder text here nicely..." %} name='keyword' id='inputsearch'>
    </div>
  </form>

The views.py file:

def search_home(request):
    return render(request, 'search_home.html',context)

Passing it from context does not work either.

from django.utils.translation import gettext_lazy as lazy_
def search_home(request):
    context = {'placeholder_search':lazy_('some placeholder here to be translated...')}
    return render(request, 'search_home.html',context)

  <form class="form_search">
    {% csrf_token %}
    <div class="form-group input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-search" style="color:black"></i></span>
        <input type="search" class="form-control home_search rounded-end" placeholder={{context.placeholder_search}} name={{context.placeholder_search}} id='inputsearch'>
    </div>
  </form>

Anyone knows how to translate a placeholder?


Solution

  • I think what you need to do is place quotation marks around the translated message like this:

    <input type="search" class="form-control home_search rounded-end" placeholder="{% translate "placeholder text here nicely..." %}" name='keyword' id='inputsearch'>