Django 翻译输入字段占位符

问题描述 投票:0回答:1

我在搜索栏表单中有一个占位符。我想将其翻译成用户选择的语言。我尝试使用

{% translate 'message to translate' %}
但只得到了栏中显示的第一个单词(也许引号有问题?)。

html
文件:

  <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>

views.py
文件:

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

context
传递它也不起作用。

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>

有人知道如何翻译占位符吗?

django internationalization translation django-i18n
1个回答
0
投票

我认为你需要做的就是在翻译后的消息周围加上引号,如下所示:

<input type="search" class="form-control home_search rounded-end" placeholder="{% translate "placeholder text here nicely..." %}" name='keyword' id='inputsearch'>
© www.soinside.com 2019 - 2024. All rights reserved.