Support defining messages in multiple languages
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
i18n.min.js(located in the
distdirectory) to your page. Ensure that it's placed after
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.
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 examples.
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
In order to use them, you only need to include the language file:
Using literal object
Using a callback function
You also can use a callback function that returns the literal object as above:
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
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.
- Fix the issue when using with validators supporting dynamic message such as the stringLength
- v0.1.0: First release