amforms icon indicating copy to clipboard operation
amforms copied to clipboard

Translate the validation errors

Open ipetrov87 opened this issue 6 years ago • 1 comments

I have been using the plugin to make a contact form for one of my client. It is the great plugin and save me a lot of work. Now the client want to have the form validation messages like "Name cannot be blank." to be translated to same language that is the entire site, which is not English. I am trying to done from several hours and I am still not done. To submit the form I am using the Ajax example from here.

ipetrov87 avatar Mar 06 '18 11:03 ipetrov87

I done a very simple solution until we get the general one. So here it is if someone else need it:

Open a file craft\plugins\amforms\controllers\AmForms_SubmissionsController.php at line 201 then replace this:

if (craft()->request->isAjaxRequest()) {
    $return = array(
        'success' => false,
        'errors' => $submission->getErrors()
    );
    $this->returnJson($return);
}

with this:

if (craft()->request->isAjaxRequest()) {

    $translated = $submission->getErrors();
    foreach ($translated as $field => $errors) {
        foreach ($errors as $error => $errorText) {
            $translated[$field][$error] = Craft::t($errorText);
        }
    }

    $return = array(
        'success' => false,
        'errors' => $translated
    );

    $this->returnJson($return);
}

And finally add the translation of the error for each field to the translation file in craft\app\translations

Hope soon we will have a general solution to this issue.

ipetrov87 avatar Mar 06 '18 13:03 ipetrov87