View source
<?php
namespace Drupal\tmgmt_google_test\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class GoogleTranslatorTestController {
public function availableLanguages(Request $request) {
if ($response = $this
->validateKey($request)) {
return $response;
}
$response = array(
'data' => array(
'languages' => array(
array(
'language' => 'en',
),
array(
'language' => 'de',
),
array(
'language' => 'fr',
),
),
),
);
return new JsonResponse($response);
}
protected function validateKey(Request $request) {
if ($request
->get('key') != 'correct key') {
return $this
->trigger_response_error('usageLimits', 'keyInvalid', 'Bad Request');
}
}
public function trigger_response_error($domain, $reason, $message, $locationType = NULL, $location = NULL) {
$response = array(
'error' => array(
'errors' => array(
'domain' => $domain,
'reason' => $reason,
'message' => $message,
),
'code' => 400,
'message' => $message,
),
);
if (!empty($locationType)) {
$response['error']['errors']['locationType'] = $locationType;
}
if (!empty($location)) {
$response['error']['errors']['location'] = $location;
}
return new JsonResponse($response);
}
public function translate(Request $request) {
$this
->validateKey($request);
if (!$request->query
->has('-q')) {
$this
->trigger_response_error('global', 'required', 'Required parameter: q', 'parameter', 'q');
}
if (!$request->query
->has('source')) {
$this
->trigger_response_error('global', 'required', 'Required parameter: source', 'parameter', 'source');
}
if (!$request->query
->has('target')) {
$this
->trigger_response_error('global', 'required', 'Required parameter: target', 'parameter', 'target');
}
$translations = array(
'de' => 'Hallo Welt & willkommen',
'fr' => 'Bonjour tout le monde',
);
$response = array(
'data' => array(
'translations' => array(
array(
'translatedText' => $translations[$_GET['target']],
),
),
),
);
return new JsonResponse($response);
}
}