# Cookbook Contact Form alert and success messages translations

**URL:** <https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124>\
**Category:** Questions\
**Tags:** v3\
**Created:** [February 4, 2020, 7:39pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124 "2020-02-04T19:39:13Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![nirusu](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nirusu/32/3219_2.png) [@nirusu](https://forum.getkirby.com/u/nirusu)\
**Post date:** [February 4, 2020, 7:39pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124/1 "2020-02-04T19:39:13Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 4, 2020, 7:57pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124/2 "2020-02-04T19:57:08Z")

</div>

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

```auto
<?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.

> **[Custom language variables](https://getkirby.com/docs/guide/languages/custom-language-variables)**
>
> Learn how to define translatable variables, which can be used to translate fixed parts in your templates like buttons.

---

<div class="post-metadata">

**Author:** ![nirusu](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nirusu/32/3219_2.png) [@nirusu](https://forum.getkirby.com/u/nirusu)\
**Post date:** [February 5, 2020, 7:20pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124/3 "2020-02-05T19:20:46Z")

</div>

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 = [];
      }
```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 5, 2020, 7:24pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124/4 "2020-02-05T19:24:53Z")

</div>

What if you add a fallback?

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

```

Same for success.

---

<div class="post-metadata">

**Author:** ![nirusu](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nirusu/32/3219_2.png) [@nirusu](https://forum.getkirby.com/u/nirusu)\
**Post date:** [February 5, 2020, 7:28pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124/5 "2020-02-05T19:28:04Z")

</div>

> [@texnixe](#):
>
> t(‘form.error’, ‘ERROR’ );

Yes, then it says ‘SUCCESS’…

but not texts from the languages files.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 5, 2020, 7:29pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124/6 "2020-02-05T19:29:48Z")

</div>

Ok, I think I have to try it myself… 🚶‍♀️

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 5, 2020, 7:47pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124/7 "2020-02-05T19:47:27Z")

</div>

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

 ![Screenshot 2020-02-05 at 20.59.53](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/d/da583539166e4166b2819877b4e79ea67a92a0c7.png)

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

Could you post your language files, please?

---

<div class="post-metadata">

**Author:** ![nirusu](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nirusu/32/3219_2.png) [@nirusu](https://forum.getkirby.com/u/nirusu)\
**Post date:** [February 5, 2020, 8:11pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124/8 "2020-02-05T20:11:49Z")

</div>

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 !',
  ]
];
```

---

<div class="post-metadata">

**Author:** ![nirusu](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nirusu/32/3219_2.png) [@nirusu](https://forum.getkirby.com/u/nirusu)\
**Post date:** [February 5, 2020, 8:17pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124/9 "2020-02-05T20:17:07Z")

</div>

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 = [];
      }
```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 5, 2020, 8:28pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124/10 "2020-02-05T20:28:51Z")

</div>

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](https://getkirby.com/docs/cookbook/forms/email-with-attachments#the-controller)

---

<div class="post-metadata">

**Author:** ![nirusu](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nirusu/32/3219_2.png) [@nirusu](https://forum.getkirby.com/u/nirusu)\
**Post date:** [February 5, 2020, 8:31pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124/11 "2020-02-05T20:31:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 5, 2020, 8:33pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124/12 "2020-02-05T20:33:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 5, 2020, 8:34pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124/13 "2020-02-05T20:34:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nirusu](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nirusu/32/3219_2.png) [@nirusu](https://forum.getkirby.com/u/nirusu)\
**Post date:** [February 5, 2020, 8:40pm UTC](https://forum.getkirby.com/t/cookbook-contact-form-alert-and-success-messages-translations/17124/14 "2020-02-05T20:40:47Z")

</div>

You’re kidding me? 😂😂😂😂 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 😉 Remember, don’t drink and code !

thank youuuuuu
