Multilanguage eForm
From MODx Wiki
The Challange
So you have a site in fifteen languages. You want a simple contact form. Each form needs to have its label/prompts in its own language. Does that mean fifteen eForm view template chunks? No! Fortunately for us, eForm has some built-in magic called events that can solve all your problems.
The Solution
The solution is in two parts. The first part is to add your label/prompt strings to the relevant language files in assets/snippets/eform/lang:
$_lang['youremail'] = "Adresse e-mail"; $_lang['yourname'] = "Nom"; $_lang['yourphone'] = "Téléphone"; $_lang['yourcompany'] = "Entreprise"; $_lang['yourmessage'] = "Message"; $_lang['sendmessage'] = "Envoyer";
The magic part is in using eForm's eFormOnBeforeFormMerge event. Create a new snippet, name it something like setPrompts. In this snippet you must put a function for eForm:
<?php function setPrompts(&$fields) { global $_lang; $fields['yourname'] = $_lang['yourname']; $fields['youremail'] = $_lang['youremail']; $fields['yourphone'] = $_lang['yourphone']; $fields['yourcompany'] = $lang['yourcompany']; $fields['yourmessage'] = $_lang['yourmessage']; $fields['sendmessage'] = $_lang['sendmessage']; return true; } ?>
Notice the "return true;" at the end. The function MUST have this for eForm to continue processing. If you put "return false;" or forget to put it at all, eForm will stop in its tracks and do nothing.
Usage
To use this, call your setPrompts snippet before your eForm snippet, then add the event and your function name to the eForm snippet call (in this case for the French contact form page):
[[setPrompts]] [!eForm? &formid=`cfForm` &tpl=`contactForm` ... &eFormOnBeforeFormMerge=`setPrompts` &language=`french`!]
Now you can use just one template chunk named contactForm (notice the id="cfForm" in the form tag; this is necessary since you specified &formid=`cfForm` in the eForm snippet call):
<div id="cfContainer"> <span style="color:red">[+validationmessage+]</span> <form id="cfForm" method="post" action="[~[*id*]~]" enctype="multipart/form-data"> <fieldset id="cfSet"> <label for="cfName">[+yourname+]: <input name="name" id="cfName" class="text" type="text" eform="[+yourname+]::1:" /> </label> <label for="cfEmail"> [+youremail+]: <input name="email" id="cfEmail" class="text" type="text" eform="[+youremail+]:email:1" /> </label> <label for="cfPhone"> [+yourphone+]: <input name="phone" id="cfPhone" class="text" type="text" eform="[+yourphone+]::1" /> </label> <label for="cfCompany"> [+yourcompany+]: <input name="company" id="cfCompany" class="text" type="text" eform="[+yourcompany+]::1" /> </label> <label for="cfMessage">[+yourmessage+]: <br /> <textarea name="message" id="cfMessage" rows="4" cols="20" eform="[+yourmessage+]::1"></textarea> </label> <input type="submit" name="contact" id="cfContact" class="button" value="[+sendmessage+]" /> </fieldset> </form> </div>
Style as you like with CSS, and it'll work like magic in all fifteen languages.
