Skip to content

Commit

Permalink
Translate custom message keys
Browse files Browse the repository at this point in the history
Resolves #161
  • Loading branch information
brandonkelly committed Apr 11, 2022
1 parent 1aa4708 commit b3977c2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes for Contact Form

## Unreleased

### Changed
- Custom message fields’ labels can now be translated using the `site` translation category. ([#161](https://github.com/craftcms/contact-form/pull/161))

## 2.3.0 - 2022-01-21

### Changed
Expand Down
14 changes: 11 additions & 3 deletions src/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use craft\contactform\events\SendEvent;
use craft\contactform\models\Submission;
use craft\elements\User;
use craft\helpers\ArrayHelper;
use craft\helpers\FileHelper;
use craft\helpers\StringHelper;
use craft\mail\Message;
Expand Down Expand Up @@ -194,9 +195,16 @@ public function compileTextBody(Submission $submission): string
$fields[Craft::t('contact-form', 'Email')] = $submission->fromEmail;

if (is_array($submission->message)) {
$body = $submission->message['body'] ?? '';
$fields = array_merge($fields, $submission->message);
unset($fields['body']);
$messageFields = array_merge($submission->message);
$body = ArrayHelper::remove($messageFields, 'body', '');
$messageKeys = array_map(function($key) {
if (is_string($key)) {
return Craft::t('site', $key);
}
return $key;
}, array_keys($messageFields));
$otherBodyFields = array_combine($messageKeys, $messageFields);
$fields += $otherBodyFields;
} else {
$body = (string)$submission->message;
}
Expand Down

0 comments on commit b3977c2

Please sign in to comment.