public function translateRemoteAction()
 {
     set_time_limit(0);
     // Get params
     $this->view->source = $source = $this->_getParam('source');
     $this->view->target = $target = $this->_getParam('target');
     $this->view->batchCount = $batchCount = $this->_getParam('batchCount', 50);
     $this->view->retranslate = $retranslate = $this->_getParam('retranslate');
     $this->view->file = $file = $this->_getParam('file');
     $this->view->offset = $offset = $this->_getParam('offset', 0);
     // Check params
     // Check source
     if (empty($source) || !is_string($source)) {
         throw new Engine_Exception('Invalid source data type');
     }
     if (!Zend_Locale::isLocale($source)) {
         $this->view->status = false;
         $this->view->error = 'The source language does not appear to be a valid locale.';
         return;
     }
     if (!Engine_Service_GTranslate::isAvailableLanguage($source)) {
         $this->view->status = false;
         $this->view->error = 'The source language does not appear to be a available for translation.';
         return;
     }
     // Check source dir
     $sourceDir = $this->_languagePath . '/' . $source;
     if (!is_dir($sourceDir)) {
         $this->view->status = false;
         $this->view->error = 'The source language does not appear to exist.';
         return;
     }
     // Check target
     if (empty($target) || !is_string($target)) {
         $this->view->status = false;
         $this->view->error = 'Invalid target data type.';
         return;
     }
     // See if we need to expand target
     if (in_array($target, array('all', 'all-translated', 'all-untranslated'))) {
         $form = new Core_Form_Admin_Language_Translate();
         $targetMultiOptions = $form->target->options;
         if ($target == 'all-translated') {
             $targets = $targetMultiOptions['Translated'];
         } else {
             if ($target == 'all-untranslated') {
                 $targets = $targetMultiOptions['Untranslated'];
             } else {
                 $targets = array_merge($targetMultiOptions['Untranslated'], $targetMultiOptions['Translated']);
             }
         }
         // Meh
         $this->view->status = true;
         $this->view->state = 'listTargets';
         $this->view->targets = $targets;
         return;
     }
     // Check target
     if (!Zend_Locale::isLocale($source)) {
         $this->view->status = false;
         $this->view->error = 'The target language does not appear to be a valid locale.';
         return;
     }
     if (!Engine_Service_GTranslate::isAvailableLanguage($source)) {
         $this->view->status = false;
         $this->view->error = 'The target language does not appear to be a available for translation.';
         return;
     }
     // Check target dir
     $targetDir = $this->_languagePath . '/' . $target;
     if (!is_dir($targetDir)) {
         // try to create
         if (!@mkdir($targetDir, 0777, true)) {
             $this->view->status = false;
             $this->view->error = 'The target language pack does not exist and it was not possible to create it.';
             return;
         }
     }
     // Check files
     $sourceFiles = array();
     $it = new DirectoryIterator($sourceDir);
     foreach ($it as $file) {
         // Ignore dirs (duh)
         if (!$file->isFile()) {
             continue;
         }
         if (strtolower(substr($file->getFilename(), -4)) !== '.csv') {
             continue;
         }
         // Ignore files that have already been translated (at least for now)
         //if( file_exists($targetDir . '/' . $file->getFilename()) ) continue;
         $files[] = basename($file->getPathName());
     }
     if (empty($files)) {
         $this->view->status = false;
         $this->view->error = 'No source files.';
         return;
     }
     // See if we need to expand file
     if (empty($file)) {
         $this->view->status = true;
         $this->view->state = 'listFiles';
         $this->view->files = $files;
         return;
     } else {
         if (!in_array($file, $files)) {
             $this->view->status = false;
             $this->view->error = 'Not a valid file.';
             return;
         }
     }
     $sourceFile = $sourceDir . '/' . $file;
     $targetFile = $targetDir . '/' . $file;
     // Init translate API
     $languageApi = new Engine_Service_GTranslate();
     $languageApi->setRequestType('curl');
     // Init reader and writer
     $reader = new Engine_Translate_Writer_Csv($sourceFile);
     $writer = new Engine_Translate_Writer_Csv($targetFile);
     // Get messages and diff
     $sourceMessages = $reader->getTranslations();
     $targetMessages = $writer->getTranslations();
     $deltaMessages = array_diff_key($sourceMessages, $targetMessages);
     // Take the batchCount of messages
     // In retranslate mode, we need to go by offset
     $currentMessages = array_slice($deltaMessages, $retranslate ? $offset : 0, $batchCount);
     if ($retranslate) {
         $this->view->newOffset = $offset = $offset + count($currentMessages);
     }
     // If it's empty, we're done with the file
     if (!$retranslate && empty($deltaMessages) || empty($currentMessages)) {
         $this->view->status = true;
         $this->view->state = 'fileComplete';
         return;
     }
     // Process plurals into normal variables temporarily
     $currentKeys = array();
     $currentValues = array();
     foreach ($currentMessages as $key => $value) {
         if (is_array($value)) {
             foreach ($value as $val) {
                 $currentKeys[] = $key;
                 $currentValues[] = $this->_escape($val);
             }
         } else {
             $currentKeys[] = $key;
             $currentValues[] = $this->_escape($value);
         }
     }
     // Send to google
     $response = (array) $languageApi->query($source, $target, $currentValues);
     if (!$response || count($response) !== count($currentValues)) {
         $this->view->status = false;
         $this->view->error = 'Translation failed';
         return;
     }
     // Process response back into plurals
     $resultantMessages = array();
     foreach ($currentKeys as $index => $key) {
         //$previousValue = $currentValues[$index];
         $value = $this->_unescape($response[$index]);
         if (isset($resultant[$key])) {
             if (!is_array($resultant[$key])) {
                 $resultant[$key] = array($resultant[$key]);
             }
             $resultant[$key][] = $value;
         } else {
             $resultant[$key] = $value;
         }
     }
     // Write to file
     $writer->setTranslations($resultantMessages);
     $writer->write();
     // Send back statistics
     $this->view->status = true;
     $this->view->sourceCount = count($sourceMessages);
     $this->view->targetCount = count($targetMessages);
     $this->view->untranslatedCount = count($deltaMessages);
     $this->view->translatedCount = count($resultantMessages);
 }