Support multiple locales for error messages
As you already knew, you can use the
message option to indicate error message for each validator:
This L10n plugin allows to define messages in different languages. The following piece of code is the starting point to use the L10n plugin:
The sample code above assumes that the FormValidation files are placed inside the
vendors directory. You might need to change the path depending on where you place them on the server.
The languages are distinguished by locales. A locale is combination of
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 L10n plugin 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. In order to use them, you only need to include the language file:
Using literal object
Map the locale with associating message for particular validator:
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 setLocale() method:
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.
- v1.0.0: First release