As you already knew, you can use the message option to indicate error message for each validator:
This i18n add-on allows to define messages in different languages. Below is a step by step instruction showing how to use the i18n add-on.
Including the add-on
- Download the i18n add-on
- Include
i18n.min.js
(located in thedist
directory) to your page. Ensure that it's placed afterformValidation(.min).js
Calling the add-on
It's possible to call the add-on in both programmatic and declarative ways:
If you want to use multiple add-ons, just simply separate them by a comma in data-fv-addons attribute:
data-fv-addons="i18n, mandatoryIcon, recaptcha2"
You even don't need to set any option for the i18n add-on.
Defining messages
The languages are distinguished by locales. A locale is combination of
countrycode_LANGUAGECODE
. Here countrycode and
LANGUAGECODE are the ISO 3166 country and language codes in lowercase
and uppercase respectively.
en_US (default), fr_FR, de_DE, vi_VN are some ejemplos.
The i18n add-on provides three ways to define messages in different languages.
Using language packages
There are many supported language packages which provide the translation of default validator message in given language.
All of them are placed inside the js/language
directory.
In order to use them, you only need to include the language file:
Using literal object
By default, message
must be a string. With the i18 add-on, you can use a
Javascript literal object which maps the locale with associating message:
Using a callback function
You also can use a callback function that returns the literal object as above:
Switching languages
The previous section introduces various ways to define the message in different locales.
To switch messages between them (via a switcher control, for example), you need to call
the setLocale()
method:
Example
The following example uses all of three ways above to define the messages in different languages. It's up to you to choose any way in a multilingual website.
Change log
-
v0.1.1:
- Fix the issue when using with validators supporting dynamic message such as the stringLength
- v0.1.0: First release