tmgmt_google_test.module in TMGMT Translator Google 7
Main bootstrap file of the tmgmt_google_test module.
File
tests/tmgmt_google_test.moduleView source
<?php
/**
* @file
* Main bootstrap file of the tmgmt_google_test module.
*/
/**
* Implements hook_menu().
*
*/
function tmgmt_google_test_menu() {
$menu = array();
$menu['tmgmt_google_test/languages'] = array(
'title' => 'Get available languages',
'page callback' => 'tmgmt_google_test_available_languages',
'access callback' => TRUE,
'file' => 'tmgmt_google_test.module',
'type' => MENU_CALLBACK,
);
$menu['tmgmt_google_test'] = array(
'title' => 'Translate',
'page callback' => 'tmgmt_google_test_translate',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$menu['tmgmt_google_test_http_request'] = array(
'title' => 'Test http request',
'page callback' => 'tmgmt_google_test_http_request',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'weight' => 0,
);
return $menu;
}
/**
* Mok service to translate request.
*/
function tmgmt_google_test_translate() {
tmgmt_google_test_key_validate();
if (empty($_GET['_q'])) {
tmgmt_google_test_trigger_response_error('global', 'required', 'Required parameter: q', 'parameter', 'q');
}
if (empty($_GET['source'])) {
tmgmt_google_test_trigger_response_error('global', 'required', 'Required parameter: source', 'parameter', 'source');
}
if (empty($_GET['target'])) {
tmgmt_google_test_trigger_response_error('global', 'required', 'Required parameter: target', 'parameter', 'target');
}
$translations = array(
'de' => 'Hallo Welt',
'fr' => 'Bonjour tout le monde',
);
$response = array(
'data' => array(
'translations' => array(
array(
'translatedText' => $translations[$_GET['target']],
),
),
),
);
drupal_json_output($response);
}
/**
* Mok service to get available languages.
*/
function tmgmt_google_test_available_languages() {
tmgmt_google_test_key_validate();
$response = array(
'data' => array(
'languages' => array(
array(
'language' => 'en',
),
array(
'language' => 'de',
),
array(
'language' => 'fr',
),
),
),
);
drupal_json_output($response);
}
/**
* Key validator helper.
*/
function tmgmt_google_test_key_validate() {
if ($_GET['key'] != 'correct key') {
tmgmt_google_test_trigger_response_error('usageLimits', 'keyInvalid', 'Bad Request');
}
}
/**
* Helper to trigger mok response error.
*
* @param string $domain
* @param string $reason
* @param string $message
* @param string $locationType
* @param string $location
*/
function tmgmt_google_test_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;
}
drupal_json_output($response);
}
/**
* Callback to test if we can do http request from and to the same server.
*/
function tmgmt_google_test_http_request() {
$url = url('tmgmt_google_test/languages', array(
'absolute' => TRUE,
));
$response = drupal_http_request($url);
return $url . '<pre>' . print_r($response, TRUE) . '</pre>';
}
Functions
Name | Description |
---|---|
tmgmt_google_test_available_languages | Mok service to get available languages. |
tmgmt_google_test_http_request | Callback to test if we can do http request from and to the same server. |
tmgmt_google_test_key_validate | Key validator helper. |
tmgmt_google_test_menu | Implements hook_menu(). |
tmgmt_google_test_translate | Mok service to translate request. |
tmgmt_google_test_trigger_response_error | Helper to trigger mok response error. |