简体   繁体   English

切换烧瓶wtforms形式的标签语言

[英]Toggle language of labels in flask wtforms form

I have a series of wtforms written in a flask app. 我在flask应用程序中编写了一系列wtforms。 One of my pages in my app will be used by people speaking English and Spanish. 我的应用程序中的我的页面之一将被说英语和西班牙语的人使用。 I would like to find a clean way of toggling the form element labels between English and Spanish with a toggle button on the webpage. 我想找到一种使用网页上的切换按钮在英语和西班牙语之间切换表单元素标签的干净方法。

I found a nice solution to toggle languages using the HTML lang attribute here (2nd answer down, by J Grover): 我在这里找到了一个使用HTML lang属性切换语言的好方法(第二答案,由J Grover撰写):

Using Javascript to change website language 使用Javascript更改网站语言

This encloses each language in a <span> element and then simply hides the one we don't want to see. 这会将每种语言包含在<span>元素中,然后仅隐藏我们不希望看到的语言。

My problem is that the labels for the fields on my forms are coming from wtforms objects. 我的问题是表单上字段的标签来自wtforms对象。 I am unsure how to include multiple languages in my current setup. 我不确定如何在当前设置中包括多种语言。 See below for an example of where I am now: 下面是我现在所在位置的示例:

Form 形成

class RoofCheck(FlaskForm):                                                                                                                                                                                              
    """                                                                                                                                                                                                                      
    Class holding the form inputs for a roof check                                                                                                                                                                                       
    """                                                                                                                                                                                                                      
    roofstate = RadioField('Roof Status',                                                                                                                                                                               
                           validators=[validators.DataRequired('Please select roof closed or open')],                                                                                                                        
                           choices=[('closed', 'Closed'), ('open', 'Open')])                                                                                                                                                 
    comments = TextAreaField('Comments on roof status',                                                                                                                                                                    
                             [validators.optional(), validators.length(max=390)],                                                                                                                                          
                             render_kw={"placeholder": "Enter comments here",                                                                                                                                              
                                        "rows": 4,                                                                                                                                                                         
                                        "style": "min-width: 100%"})                                                                                                                                                       
    submit = SubmitField('Submit') 

HTML 的HTML

<div id='roof_check' class='col-md-6 padding-0'>                                                                                                                                                                        
    <form id="roof_check_form" action={{ url_for('roof_check')}} method="post" name="roof">                                                                                                                                   
        <fieldset class='form_group'>                                                                                                                                                                                    
            {{ form1.hidden_tag() }}                                                                                                                                                                                     
            <legend class='text-center'>                                                                                                                                                                                 
                Daily visual check of the roof                                                                                                                                                          
            </legend>                                                                                                                                                                                                    
            {% for subfield in form1.roofstate %}                                                                                                                                                                        
                <div class='form-check'>                                                                                                                                                                                 
                    {{ subfield }} &nbsp;                                                                                                                                                                                
                    {{ subfield.label(class_="form-check-label") }} <br/>                                                                                                                                                
                </div>                                                                                                                                                                                                   
            {% endfor %}                                                                                                                                                                                                 
            <div class='form-check'>                                                                                                                                                                                     
                {{ form1.comments }}                                                                                                                                                                                   
            </div>                                                                                                                                                                                                       
            <div class='form-check'>                                                                                                                                                                                     
                {% with messages = get_flashed_messages(category_filter=["form1"]) %}                                                                                                                                    
                    {% if messages %}                                                                                                                                                                                    
                        {% for message in messages %}                                                                                                                                                                    
                            <div> {{ message }} </div>                                                                                                                                                                   
                        {% endfor %}                                                                                                                                                                                     
                    {% endif %}                                                                                                                                                                                          
                {% endwith %}                                                                                                                                                                                            
                {% for message in form1.roofstate.errors %}                                                                                                                                                              
                    <div> {{ message }} </div>                                                                                                                                                                           
                {% endfor %}                                                                                                                                                                                             
                <div style="padding-top: 5px;">                                                                                                                                                                          
                    {{ form1.submit(class_="btn btn-primary") }}                                                                                                                                                        
                </div>                                                                                                                                                                                                   
            </div>                                                                                                                                                                                                       
        </fieldset>                                                                                                                                                                                                      
    </form>                                                                                                                                                                                                              
</div> 

I am not sure where to begin adding multiple labels to the RoofCheck class form objects. 我不确定从哪里开始向RoofCheck类表单对象添加多个标签。 Any advice on how to insert support for two languages here would be great. 关于如何在此处插入对两种语言的支持的任何建议都很好。 Thanks in advance. 提前致谢。

So it turns out that I can just add html directly to the label and place the language attribute there, then use the javascript solution in the link above to toggle the language. 因此,事实证明,我可以直接将html直接添加到标签,然后将language属性放置在标签上,然后使用上面链接中的javascript解决方案来切换语言。 For example: 例如:

roof_status_text = ("<span lang='en'>Roof Status</span>"
                    "<span lang='es'>Estado del Techo</span>")
open_text = ("<span lang='en'>Open</span>"
             "<span lang='es'>Abierto</span>")
closed_text = ("<span lang='en'>Closed</span>"
               "<span lang='es'>Cerrado</span>")
roofstate = RadioField(roof_status_text,
                       validators=[validators.DataRequired('Please select roof closed or open')],                                                                                                                        
                       choices=[('closed', closed_text),
                                ('open', open_text)])  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM