Friday, May 24, 2013

How to handle multilingual error messages for jQuery validation plugins

In How to customize error messages for jQuery validation plugin guides us customize error messages for jQuery validation plugin.
But nowadays, multilingual is a requirement for most of the application.
Thus on top of customized messages, we need to provide multilingual support for the error messages.

This article will show an easy way to provide multilingual support for jQuery validation plugin.

The trick is to keep our customized error messages in resource bundle.

Steps:
1.  write the customized error messages in our application's resource bundle. eg, messages.properties
e.g.
invalid_email=Invalid email format


2. use the resource key for the customized error message
 <script>
  $(document).ready(function(){
    $("#myform").validate({
  rules: {
      'myform:emailAddr': {
      required: true,
      email: true
    }
  },
  messages: {
      'myform:emailAddr': {
        email: "#{msgs.invalid_email}"
    }
  }
});
  });
  </script>

3. prepare the resource bundle in different languages, eg. messages_fr.properties, messages_zh.properties, etc.

4. when system locale changed, it will automatically retrieve the messages from appropriate resource bundle.



Done!!


18 comments:

  1. This doesn't work is the script is located in a separate JS file. It outputs it as string. Any ideas?

    ReplyDelete
    Replies
    1. Sorry, I do not get your problem, can you explain in more details.

      Delete
    2. Hi Kian,

      Sorry had a typo and could not edit what I wrote.

      I meant to say that putting the resource key "#{msgs.invalid_email}" for the message doesn't render it properly on the browser as "Invalid email format" instead, it's rendering it as a string, so when the validation kicks in. It reads is as "#{msgs.invalid_email}" on the browser and not "Invalid email format".

      I have the script as a separate Javascript file and not within the HTML page.

      Delete
    3. Hi John Mark,

      When I moved the javascript into a separate js file as you said, I had the same problem.
      I have no ideas at the moment how to resolve this issue as well.

      Delete
    4. Please update this blog post when you do find the solution. I find your site a great resource! :)

      Delete
  2. In either case, if your target group doesn't get the message, you've likely sent an inappropriate one. Also, that can have critical results.
    whatsapp gb

    ReplyDelete
  3. I am facing the issue in WPML plugin of wordpress, then I installed Yo WhatsApp which fixes all the error.

    ReplyDelete
  4. Thanks for sharing this, it was helpful. Keep sharing such content in future. Also checkout Nulls Clash.

    ReplyDelete
    Replies
    1. The information you have shared here is very helpful. I really like and appreciate your work. Thanks again for it, keep us updated in future!

      Delete
  5. Thanks for the detailed article on this topic. I would like to see more such awesome articles from you.

    ReplyDelete
  6. Thanks for sharing this valuable piece of information. Keep sharing more such awesome articles in the future. Goodbye!

    ReplyDelete
  7. Your site is the best source of information, it was very helpful. Thanks for sharing.

    ReplyDelete
  8. Your site is the best source of information, it was very helpful. Thanks for sharing. Keep sharing such things.

    ReplyDelete
  9. thanks for share this detailed article on notes of my java career,Can you tell me more about this via yo whatsapp.

    ReplyDelete
  10. Hey, thank you a lot for sharing this article with us. I can’t say how grateful we are to read this. Also, I would love to share it with my friends and family, who are interested.

    ReplyDelete
  11. I am really extremely glad to visit your blog. Keep doing awesome! Easy Way to Become An Administrator on Roblox, also Forget the password of your existing account? Without wasting time, get back your lost password using Roblox Password Finder. Let's move forward with the solution.

    ReplyDelete

LinkWithin

Related Posts Plugin for WordPress, Blogger...