Skip to content

Commit

Permalink
Add i18n to the email backend
Browse files Browse the repository at this point in the history
This makes the feedback form pass through
the language to the email backend so that
localized errors and thanks messages can be
send back to the frontend.
  • Loading branch information
addshore committed Apr 27, 2016
1 parent ed1593d commit 8022da9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
19 changes: 14 additions & 5 deletions backend/src/Actions/FeedbackAction.php
Expand Up @@ -5,22 +5,31 @@

class FeedbackAction {
private $app;
private $i18n;
private $errors = [];

function __construct( \Silex\Application $app ) {
$this->app = $app;
$request = $app['request'];

$lang = !empty( $request->get( 'lang' ) ) ? $request->get( 'lang' ) : 'de';
$langFile = __DIR__ . '/../../../i18n/' . $lang . '/i18n.json';
if( !file_exists( $langFile ) ) {
$langFile = __DIR__ . '/../../../i18n/de/i18n.json';
}
$this->i18n = json_decode( file_get_contents( $langFile ), true );

if ( $this->requestValid( $app['request'] ) ) {
$this->sendMail(
$app['request']->get( 'name' ),
$app['request']->get( 'feedback' )
$request->get( 'name' ),
$request->get( 'feedback' )
);
}
}

private function requestValid( \Symfony\Component\HttpFoundation\Request $request ) {
if ( empty( $request->get( 'name' ) ) || empty( $request->get( 'feedback' ) ) ) {
$this->errors[] = 'Alle Felder des Feedback-Formulars müssen ausgefüllt werden.';
$this->errors[] = $this->i18n['error']['feedback-blank-fields'];
return false;
}

Expand All @@ -37,12 +46,12 @@ private function sendMail( $sender, $text ) {
try {
$this->app['mailer']->send( $message );
} catch ( Exception $e ) {
$this->errors[] = 'Beim Senden der Nachricht ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.';
$this->errors[] = $this->i18n['error']['feedback-generic'];
}
}

private function success() {
return [ 'message' => 'Vielen Dank für Ihr Feedback!' ];
return [ 'message' => $this->i18n['index']['feedback-thanks'] ];
}

public function getResponse() {
Expand Down
5 changes: 4 additions & 1 deletion i18n/de/i18n.json
Expand Up @@ -13,6 +13,7 @@
"feedback-name": "Name",
"feedback-body": "Feedback",
"feedback-submit": "Abschicken",
"feedback-thanks": "Vielen Dank für Ihr Feedback!",
"feedback": "Feedback",
"about": "Über das Tool",
"legal": "Impressum und Datenschutz",
Expand All @@ -24,7 +25,9 @@
"licence-unsupported": "Leider wird die ermittelte Lizenz des Bildes von dieser Anwendung nicht unterstützt.",
"url-invalid": "Die angegebene Internetadresse konnte nicht verarbeitet werden.",
"licence-not-recognized": "Es tut uns leid, aber dieses Bild wird aufgrund des Lizenzformats derzeit noch nicht unterstützt.",
"send-feedback": "Beim Senden ist etwas schiefgelaufen. Bitte versuche es erneut."
"send-feedback": "Beim Senden ist etwas schiefgelaufen. Bitte versuche es erneut.",
"feedback-generic": "Beim Senden der Nachricht ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.",
"feedback-blank-fields": "Alle Felder des Feedback-Formulars müssen ausgefüllt werden."
},
"info-box": {
"private-use": "Die Angabe eines Lizenzhinweises ist bei manchen privaten Nutzungen nicht nötig.",
Expand Down
2 changes: 2 additions & 0 deletions index.base.html
Expand Up @@ -157,6 +157,8 @@ <h4 class="modal-title">i18n.index.feedback</h4>
</div>
</div>

<input type="hidden" name="lang" value="i18n.lang">

<div class="modal-footer">
<button type="submit" class="green-btn">i18n.index.feedback-submit</button>
</div>
Expand Down
1 change: 1 addition & 0 deletions index.php
Expand Up @@ -12,6 +12,7 @@

// Get the base html to output
$html = file_get_contents( __DIR__ . '/index.base.html' );
$html = str_replace( 'i18n.lang', $lang, $html );

// If we have any i18n data replace the i18n codes with new strings
if ( $i18nData && isset( $i18nData['index'] ) ) {
Expand Down

0 comments on commit 8022da9

Please sign in to comment.