首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring和jQuery验证:如何使用本地化字符串?

问Spring和jQuery验证:如何使用本地化字符串?
EN

Stack Overflow用户
提问于 2016-06-17 11:54:37
回答 2查看 835关注 0票数 0

以这个表格为例:

代码语言:javascript
复制
<form id="login_form" method="post" action="/rest/auth/auth">

    <div id="login_email" class="row margin-top-50">
        <div class="col-md-12" id="input_container">
            <input id="login_email_input" class="input_edit input_edit_img_width" type="email" name="email" th:placeholder="#{label.common_email}"></input>
            <img class="input_edit_img" src="/img/ic_login_user.png"></img>
        </div>
    </div>

    <div id="login_password" class="row margin-top-15">
        <div class="col-md-12" id="input_container">
            <input id="login_password_input" class="input_edit input_edit_img_width" type="password" name="password" th:placeholder="#{label.common_password}"></input>
            <img class="input_edit_img" src="/img/ic_login_pw.png"></img>
        </div>
    </div>

    <div class="row margin-top-15">
        <div class="col-md-12">
            <div class="checkbox_square">
                <input id="check_password_show" type="checkbox" value=""></input>
                <label for="check_password_show"></label>
                <span th:text="#{label.common_password_show}"></span>
            </div>
        </div>
    </div>

    <div class="row margin-top-15">
        <div class="col-md-12">
            <button id="login_submit" type="submit" class="btn btn-default btn-blue" th:text="#{label.login}"></button>
        </div>
    </div>

</form>

如您所见,im使用本地化消息(例如th:placeholder="#{label.common_email}" )。

现在我想使用jQuery验证,我确实需要像这样使用来自messages.properties的本地化字符串:

代码语言:javascript
复制
$("#login_form").validate({
    rules: {
        email: {
            required: true,
            email: true
        },
        password: {
            required: true
            /*minlength: 6,
            maxlength: 20*/
        }
    },
    messages: {
        email: {
            required: "#{label.input_missing_email}",
            email: "#{label.input_missing_email_valid}"
        },
        password: {
            required: "#{label.input_missing_password}",
            minlength: "#{label.input_missing_password_valid}"
        }
    }
});

有任何方法可以从客户端javascript访问我的spring本地化消息吗?或者,如果我必须使用本地化字符串,那么我应该使用不同的方法进行验证吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-17 12:16:50

您不需要在javascript级别从您的messages.properties中检索您的地区字符串,您可以很容易地做到这一点,如果您仅将您的地区字符串保存在HTML中,下面我将这样做:

HTML:

  • 在每个输入之后分别创建<span>或<div> (如果您想在该元素之后显示一些msg,一些验证类)
  • 将所有地区字符串保存在html中的<span>和<div>中。

联署材料:

  • 当相应的表单输入引发<span>验证时,只需显示/隐藏相应的<div>或jQuery。
票数 1
EN

Stack Overflow用户

发布于 2016-06-17 12:23:05

jQuery.i18n.properties是一个轻量级的jQuery插件,用于从‘.properties’文件中提供javascript的国际化,就像在javascript中一样。它根据所提供的语言和国家代码加载和分析资源包(.properties)。更多

假设

代码语言:javascript
复制
label.delete.success =Successfully Deleted
label.delete.failure=Error in success

是您的messages.properties属性文件。

然后jQuery.i18n.prop(label.delete.success)从javascript文件返回Successfully Deleted。

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37880937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档