Cookbook Contact Form alert and success messages translations

Hi,

I followed the example in the cookbook for a basic contact form and it works.
Questions is: how do I go about translating the error message and success message?

I assume it should be in this part of the code?

} catch (Exception $error) {
        $alert['error'] = "The form could not be sent";
      }

      // no exception occured, let's send a success message
      if (empty($alert) === true) {
        $success = 'Your message has been sent, thank you. We will get back to you soon!';
        $data = [];
      }

but I take it I can’t just use the “t(‘error’)” function that’s used elsewhere to have translations show up…?

A pointer would be great! Thanks!

In this case the strings are custom strings, but you can translate them by using language variables in your language files:

<?php

return [
  'code' => 'en',
  'default' => true,
  'direction' => 'ltr',
  'locale' => 'en',
  'name' => 'English',
  'url' => '/',
  'translations' => [
    'form.error' => 'The form could not be sent',
    'form.succes' => 'Your message has been sent, thank you.',
  ]
];

The use t('form.error') instead of the hardcoded string.

1 Like

Thank you! the form works but now it doesn’t show the error or success message… it resets the form or something…

this is what I did with the t() thingies.

} catch (Exception $error) {
        $alert['error'] = t('form.error');
      }

      // no exception occured, let's send a success message
      if (empty($alert) === true) {
        $success = t('form.success');
        $data = [];
      }

What if you add a fallback?

$alert['error'] = t('form.error', 'ERROR' );

Same for success.

Yes, then it says ‘SUCCESS’…

but not texts from the languages files.

Ok, I think I have to try it myself… :walking_woman:

Hm, I tested it with the job applications example, and the translation strings worked for me.

Ok, I admit, the message is not so nice…

Could you post your language files, please?

ah, but those look like the error messages when someone types something wrong. I mean the ones after sending the form…

EN

<?php

return [
  'code' => 'en',
  'default' => true,
  'direction' => 'ltr',
  'locale' => 'en_US',
  'name' => 'English',
  'url' => '/',
  'translations' => [
    'contactus' => 'contact us',
    'learn' => 'Learn more',
    'legend-send' => 'send us a message',
    'label-name' => 'name / surname',
    'label-representing' => 'representing',
    'label-email' => 'your e-mail',
    'label-message' => 'message',
    'btn-send' => 'send',
    'txt-privacy' => 'our privacy policy',
    'txt-findus' => 'find us on',
    'form.error' => 'The form could not be sent',
    'form.succes' => 'Your message has been sent, thank you.',
  ]
];

NL

<?php

return [
  'code' => 'nl',
  'default' => false,
  'direction' => 'ltr',
  'locale' => 'nl_NL',
  'name' => 'Dutch',
  'translations' => [
    'contactus' => 'neem contact op',
    'learn' => 'Lees verder',
    'legend-send' => 'stuur ons een bericht',
    'label-name' => 'voornaam / achternaam',
    'label-representing' => 'jouw bedrijf',
    'label-email' => 'jouw e-mail adres',
    'label-message' => 'bericht',
    'btn-send' => 'verstuur',
    'txt-privacy' => 'ons privacybeleid',
    'txt-findus' => 'Vind ons op',
    'form.error' => 'Het bericht kon niet worden verzonden. Excuses.',
    'form.succes' => 'Je bericht is verstuurd. Dank je wel !',
  ]
];

this is in the controller file… for me the home controller.

} catch (Exception $error) {
        $alert['error'] = t('form.error');
      }

      // no exception occured, let's send a success message
      if (empty($alert) === true) {
        $success = t('form.success');
        $data = [];
      }
1 Like

Yes, but that’s the same stuff, after all. The message I show above is an error message set in the $alerts array of this example form, only using translation strings instead of the original hard coded messages: https://getkirby.com/docs/cookbook/forms/email-with-attachments#the-controller

weird. it seems to expect a string there… or a different notation than the t();

I just tried it with the contact form example and that works as well with the translation string. After all t() returns nothing but a string.

I just noticed you have a typo in your translation variables! succes with only one “s”.

You’re kidding me? :joy::joy::joy::joy: guess I can’t blame autocorrect here. Let me check and fix it.

yesss, thank you !!! geez, sorry about that, must be the White Russian I’m drinking :wink: Remember, don’t drink and code !

thank youuuuuu