翻译自定义jQuery验证消息 [英] Translate custom jQuery validation messages

查看:118
本文介绍了翻译自定义jQuery验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于jQuery验证,我有一个问题要问.插件.

I have a question to ask regarding jQuery validation plugin.

我已经使用本地化来更改错误消息的默认语言以西班牙语显示,但是我找不到与要翻译的自定义消息有关的任何内容.有任何线索吗?

I have used localisation to change the default language of error messages to be displayed in Spanish, but I cannot find anything regarding custom messages to be translated. Any clues?

例如

我已经包含了西班牙语的翻译文件,这是我的代码:

I've included the translation file for Spanish, and this is my code:

$('.signup-form').validate({
    lang : 'es',
    rules:{
        tandc : {required : true}
    },
    messages:{
        tandc : {required : "You have to accept terms and conditions to proceed further"}
    }
})

诸如此字段为必填"之类的默认消息以西班牙语显示,现在我想将上述消息翻译为西班牙语,但是我找不到在何处以及如何定义翻译后的文本.

Default messages like "This field is required" etc are appearing in Spanish, now I want to translate the above message to Spanish but I cannot find where and how to define the translated text.

推荐答案

插件没有翻译"任何内容.手动完成翻译,然后将这些新消息放入本地化文件覆盖默认值.

Nothing is "translated" by the plugin. Translation is done manually and then you'd place these new messages into a localization file where they over-ride the default.

在任何地方都没有没有这样的 .validate()选项 在此插件中.

There is also no such .validate() option called lang anywhere in this plugin.

要使用本地化文件,仅意味着在包含插件之后将文件包含在某个位置.

To use the localization files simply means including the file as such someplace after you include the plugin...

<script type="text/javascript" src="...path-to/jquery.validation/1.15.0/jquery.validate.js" />
<script type="text/javascript" src="...path-to/jquery-validation/localization/messages_es.js" />

然后所有默认消息都将以西班牙语显示.

Then all default messages will be in Spanish.

诸如此字段为必填"之类的默认消息以西班牙语显示,现在我想将上述消息翻译为西班牙语,但是我找不到在何处以及如何定义翻译后的文本.

Default messages like "This field is required" etc are appearing in Spanish, now I want to translate the above message to Spanish but I cannot find where and how to define the translated text.

包括西班牙文本地化文件只是告诉插件替换西班牙文本地化文件定义的默认消息.

Including the Spanish localization file simply tells the plugin to replace the default messages as defined by the Spanish localization file.

您的messages对象将覆盖这些默认消息,因此,如果您希望西班牙语消息仅对单个输入字段覆盖required规则,那么您将需要用西班牙语编写该规则. 没有内置的动态翻译功能可以即时解释您的消息.

Your messages object over-rides these default messages, so if you want a Spanish message to over-ride the required rule for just a single input field, then you'll need to write that one in Spanish. There is no built-in dynamic translation that can interpret your messages on the fly.

rules:{
    tandc : {
        required : true
    }
},
messages:{
    tandc : {
        required : "Tienes que aceptar los términos y condiciones de seguir avanzando"
    }
}

这是所使用消息的优先级:

This is the priority of messages used:

  1. 使用.validate()或类似方法中的messages对象通过规则或整个字段声明的单个文本.

  1. Any text declared for a single field by rule or entire field using the messages object within .validate() or similar method.

如果在项目#1中未定义任何内容:覆盖$.extend( $.validator.messages, {...})所定义的插件默认消息.这是本地化文件的工作方式.

If nothing defined in item #1: Over-ride plugin default messages as defined by $.extend( $.validator.messages, {...}). This is how the Localization files work.

如果项目#2中未定义任何内容:插件定义的默认消息(英语).

If nothing defined in item #2: Default messages (English) as defined by the plugin.


现在,如果您需要在.validate()初始化表单上的插件后 动态 更改messages对象定义的任何消息,使用.rules('add')方法来替代它.


Now if you need to dynamically change any message as defined by the messages object after .validate() has initialized the plugin on your form, you'll have to use the .rules('add') method to over-ride it.

$('[name="foo"]').rules('add', {
    messages: {
        required: "yo! I'm required."
    }
});

演示: jsfiddle.net/3fLkf47u/

DEMO: jsfiddle.net/3fLkf47u/

这篇关于翻译自定义jQuery验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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