如何启用 jquery 验证本地化? [英] How to enable jquery validate localization?

查看:23
本文介绍了如何启用 jquery 验证本地化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 jquery mobile 实现的注册表单.用户可以选择用德语、英语或法语填写此表单 - 有多个内部页面用于以每种语言显示表单.. 用户通过菜单按钮选择他们的首选语言.

I have a registration form implemented using jquery mobile. The user can choose to complete this form in either German, English or French - there are multiple internal pages used to display the form in each language .. The user selects their preferred language via a menu button.

我使用 jquery 验证插件来处理客户端表单验证.这工作正常.. 我想知道如何为每个语言页面上的表单启用正确的错误消息?当您从 github 下载插件时,它包含所有本地化错误代码,但我不确定如何启用它..

I'm using the jquery validate plugin to handle client-side form validation. This works fine .. I'm wondering how to enable the right error messages for the form on each language page ? When you download the plugin from github it includes all the localization error code but I'm not sure how to enable it ..

这是我用来处理为每个语言页面实现表单验证的 JavaScript 代码..

Here's the JavaScript code I'm using to handle implementing the form validation for each language page ..

如果您能帮助我为德语 & 启用正确的错误消息,我将不胜感激.法语页面..

Thanks if you can help me to enable the right error messages for the German & French pages ..

$(document).on("pageshow", "#page_deutsch", function() {

$("#register_deutsch").validate();

});

$(document).on("pageshow", "#page_english", function() {

$("#register_english").validate();

});

$(document).on("pageshow", "#page_francais", function() {

$("#register_francais").validate();

});

推荐答案

jQuery 验证插件本地化jQuery 验证插件的默认语言是英语.它可以被本地化 js 文件覆盖,例如.es, zh, fr 等请参阅此处了解支持的语言列表.

jQuery validation plugin localization The default language for jQuery validation plugin is English. It could be overridden with the localization js file, eg. es, zh, fr, etc. Please refer here for the list of supported languages.

要启用语言支持,我们只需要在我们的网页(html/jsp/xhtml)中添加其他语言的消息js.法语版

To enable languages support, we just need to add other language message js into our webpage (html/jsp/xhtml). for french

<script type="text/javascript" src="http://jzaefferer.github.io/jquery-validation/localization/messages_fr.js" />

中文

<script type="text/javascript" src="http://jzaefferer.github.io/jquery-validation/localization/messages_zh.js" />

其他语言同理,找到对应语言的js,将js添加到网页中即可.

Same for other languages, just find the appropriate language js and add the js into the webpage.

如果所需的语言不在列表中,一个可行的替代方法是将 messages_xx.js 的副本下载到项目工作区,然后将其修改为所需的语言.

if the required language is not in the list, an workable alternative is to download a copy of the messages_xx.js into project workspace, then modify it to your required language.

虽然提供了语言js,但问题是在1个网页中我们只能使用1种语言消息.如果网页中有多种语言的消息js,将使用最后一个.

Although the language js is provided, but the problem is in 1 webpage we can only use 1 type of language message. If multiple language message js in the webpage, the last will be used.

为了解决这个问题,我们可以通过系统locale动态加载消息js.

To resolve this problem, we can dynamically load the messages js by system locale.

<script type="text/javascript" src="http://jzaefferer.github.io/jquery-validation/localization/messages_<%= Locale.getDefault().getLanguage() %>.js" />

通过上面的解决方案,我们终于可以通过不同的系统区域动态处理不同语言的消息了.

with the above solution, finally we can dynamically handle different language message by different system locale.

这里还有另一种使用资源包处理多种语言的方法.

There is another way to handle multiple languages with resource bundle here.

相关的jQuery验证文章:

related jQuery validation articles:

  • 电子邮件
  • 信用卡
  • 多个表单域
  • 错误信息自定义
  • 在 JSF 中使用 jQuery 验证插件
  • 多语言错误消息
  • jQuery 验证插件中的错误处理

完成!!

这篇关于如何启用 jquery 验证本地化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆