You are here

public function GoogleTranslatorTestController::translate in TMGMT Translator Google 8

Mok service to translate request.

1 string reference to 'GoogleTranslatorTestController::translate'
tmgmt_google_test.routing.yml in tmgmt_google_test/tmgmt_google_test.routing.yml
tmgmt_google_test/tmgmt_google_test.routing.yml

File

tmgmt_google_test/src/Controller/GoogleTranslatorTestController.php, line 84
Contains \Drupal\block\Controller\CategoryAutocompleteController.

Class

GoogleTranslatorTestController
Returns autocomplete responses for block categories.

Namespace

Drupal\tmgmt_google_test\Controller

Code

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);
}