public function TMGMTGoogleTranslatorPluginController::requestTranslation in TMGMT Translator Google 7
Implements TMGMTTranslatorPluginControllerInterface::requestTranslation().
Overrides TMGMTTranslatorPluginControllerInterface::requestTranslation
File
- ./
tmgmt_google.plugin.inc, line 86 - Provides Google Translator plugin controller.
Class
- TMGMTGoogleTranslatorPluginController
- Google translator plugin controller.
Code
public function requestTranslation(TMGMTJob $job) {
// Pull the source data array through the job and flatten it.
$data = array_filter(tmgmt_flatten_data($job
->getData()), '_tmgmt_filter_data');
$translation = array();
$q = array();
$keys_sequence = array();
$i = 0;
// Build Google q param and preserve initial array keys.
foreach ($data as $key => $value) {
$q[] = $value['#text'];
$keys_sequence[] = $key;
}
try {
// Split $q into chunks of self::qChunkSize.
foreach (array_chunk($q, $this->qChunkSize) as $_q) {
// Get translation from Google.
$result = $this
->googleRequestTranslation($job, $_q);
// Collect translated texts with use of initial keys.
foreach ($result['data']['translations'] as $translated) {
$translation[$keys_sequence[$i]]['#text'] = $translated['translatedText'];
$i++;
}
}
// The translation job has been successfully submitted.
$job
->submitted('The translation job has been submitted.');
// Save the translated data through the job.
// NOTE that this line of code is reached only in case all translation
// requests succeeded.
$job
->addTranslatedData(tmgmt_unflatten_data($translation));
} catch (TMGMTGoogleException $e) {
$job
->rejected('Translation has been rejected with following error: !error', array(
'!error' => $e
->getMessage(),
), 'error');
}
}