如何启用 jquery 验证本地化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18218705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:13:06  来源:igfitidea点击:

How to enable jquery validate localization?

javascriptjqueryformsvalidationjquery-mobile

提问by Andrew Phillips

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 mobile 实现的注册表单。用户可以选择用德语、英语或法语填写此表格 - 有多个内部页面用于以每种语言显示表格。用户通过菜单按钮选择他们的首选语言。

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

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

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

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

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();

});

回答by Егор Синицин

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 herefor the list of supported languages.

jQuery 验证插件本地化 jQuery 验证插件的默认语言是英语。它可以被本地化 js 文件覆盖,例如。es, zh, fr 等。请参考这里支持的语言列表。

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

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

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

for chinese

china人

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

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

其他语言也一样,只需要找到对应语言的js,将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.

如果所需的语言不在列表中,一个可行的替代方法是将 messages_xx.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.

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

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

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

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

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

related jQuery validation articles:

相关的 jQuery 验证文章:

  • Email
  • Credit card
  • Multiple form fields
  • Error message customization
  • Using jQuery Validation Plugin in JSF
  • Multilingual error messages
  • Error handling in jQuery Validation Plugin
  • 电子邮件
  • 信用卡
  • 多个表单域
  • 错误信息自定义
  • 在 JSF 中使用 jQuery 验证插件
  • 多语言错误信息
  • jQuery 验证插件中的错误处理

Done!!

完毕!!