Sitecore 9 Forms : translating error messages client-side
A while ago a wrote a post on translating error messages in Sitecore Forms. In that post I mentioned that the validation done by jquery on the client-side was out of scope (of that post).
When a question on the subject appeared on Sitecore Stack Exchange, I started investigating. With a little help of Support the outcome was adding some custom scripts (and files). A better solution might be added in a future release but for now (9.1.1 and earlier) we'll have to do it like this. Note that my answer on SSE is a general one - this post addresses fixing it in a SXA environment.
First a small recap:
The messages entered here will be used for client- and server-side error messages. Which is nice. Just be aware that somewhere also jquery validation gets used and that can lead to strange situations. I noticed that the email validation can give two different error messages depending on what the error is.. and one of them is not translated as it comes from jquery.This is what I wrote on the translated error messages in my first post. So let's start translating that email validation!
The issue can be easily reproduced with a translated form (non-English). Start typing in an email field and you will get the English jquery message. Once you have something like "...@x" the jquery message will disappear and the Sitecore one will appear (which can be translated).
Adding the localized scripts
- Download all the jquery-validation localized messages files you need from https://github.com/jquery-validation/jquery-validation/tree/master/src/localization
- Place those files in a \sitecore modules\Web\ExperienceForms\scripts\localization folder.
- You can change the translation in the files as needed - their structure is very straight forward
- Open Sitecore Form Wrapper.cshtml in the \Views\Shared folder
- Add a script just after the @Html.RenderFormScripts(): (adapt the script for the languages you have in your environment)
<script type="text/javascript">
var lang = '@Sitecore.Context.Language.Name';
var script = document.createElement("script");
if (lang == 'nl')
{
script.src = "/sitecore%20modules/Web/ExperienceForms/scripts/localization/messages_nl.js";
$("head").append(script);
}
else if (lang == 'fr')
{
script.src = "/sitecore%20modules/Web/ExperienceForms/scripts/localization/messages_fr.js";
$("head").append(script);
}
...
</script>
Don't forget to add the added/changed files to your solution. And be aware when upgrading that you changed a Sitecore file...
This should do it. The messages are not translatable by the editors, but at least they are not English everywhere...
This should do it. The messages are not translatable by the editors, but at least they are not English everywhere...
Alternatives?
It will also work if you put the messages javascript files in the scripts folder of Forms (\sitecore modules\Web\ExperienceForms\scripts) and add the name of the messages file to the Scripts field in the form (where the other jquery js files already are). Problem with this solution is that the Scripts field is shared - meaning it is the same for all languages. So, you can add one language and that will be used for all languages (including English). Unless you have a single language site that is non-English, this is not what you want.
Why do the language-checking client-side with parser-blocking JavaScript? I changed it to this (multi-language set-up) which seems to work:
ReplyDelete@if (Sitecore.Context.Language.Name.ToLowerInvariant().Contains("nl"))
{
// script-tag that blogspot doesn't allow with defer-attribute
}