guillaumepotier / Parsley.js

Validate your forms, frontend, without writing a single line of javascript
http://parsleyjs.org
MIT License
9.04k stars 1.31k forks source link

localization custom messages #718

Open bzmw opened 10 years ago

bzmw commented 10 years ago

Currently I'm using

data-parsley-constraint-message="English sentence goes here" but now that I'm working to add localization these messages will never be translated using the i18n library because they are custom.

Is there a way to add something like

data-parsley-constraint-message-fr="Francais francais francais" or someway to do it through JS?

Specifically I'm using data-parsley-required-message=""

bzmw commented 10 years ago

Great answer on stack overflow to this question:

What it comes down to is that right now if you want to define a custom error message in each local for every input field in your app then you need to define a custom validator for each input field in your app. I guess this could be a feature request then, to add support for data-parsley-constraint-message-=""

http://stackoverflow.com/questions/25454559/parsleyjs-localization-with-data-parsley-constraint-message

Why don't you use the localization of Parsley instead of defining the messages on the inputs?

I suggest you download the localizations that you need from the source. After that, according to the docs you can load the i18n before the plugin, like this:

<script src="i18n/fr.js"></script>
<script src="i18n/it.js"></script>
<script src="parsley.min.js"></script>
<script type="text/javascript">
    window.ParsleyValidator.setLocale('fr');
</script>

Or you can load the i18n after the plugin (in that case, it will assume the last loaded localization - in the following example it will be Italian), like this:

<script src="parsley.min.js"></script>
<script src="i18n/fr.js"></script>
<script src="i18n/it.js"></script>

On my projects I typically load the needed localization based a Cookie or Session variable:

<script src="parsley.min.js"></script>
<script src="i18n/"<?= echo $lang ?>".js"></script>

With either of the options, you don't need to add data-parsley-constraint-message to each input. And when you need to change a message you can do that in the localization file.

To conclude, if you need custom validators, you can check the docs to see how you can define the different localizations.