Ejemplo n.º 1
0
 /**
  * Gets the translation targets associated with this document.
  *
  * @return array
  *   An array of Translation Target, as returned by a getDocument
  *   Lingotek API call
  */
 public function translationTargets()
 {
     $targets = array();
     if ($document = LingotekApi::instance()->getDocument($this->document_id)) {
         if (!empty($document->translationTargets)) {
             foreach ($document->translationTargets as $target) {
                 $targets[Lingotek::convertLingotek2Drupal($target->language)] = $target;
             }
         }
     }
     return $targets;
 }
Ejemplo n.º 2
0
 /**
  * Updates the local content of $target_code with data from a Lingotek Document
  *
  * @param string $lingotek_locale
  *   The code for the language that needs to be updated.
  * @return bool
  *   TRUE if the content updates succeeded, FALSE otherwise.
  */
 public function downloadTriggered($lingotek_locale)
 {
     $metadata = $this->metadata();
     $document_id = $metadata['document_id'];
     if (empty($document_id)) {
         LingotekLog::error('Unable to refresh local contents for config chunk @cid. Could not find Lingotek Document ID.', array('@cid' => $this->cid));
         return FALSE;
     }
     $api = LingotekApi::instance();
     $document_xml = $api->downloadDocument($document_id, $lingotek_locale);
     $target_language = Lingotek::convertLingotek2Drupal($lingotek_locale);
     /* FAST VERSION (git history for slow version) */
     // 1. save the dirty targets associated with given language
     $dirty_lids = self::getDirtyLidsByChunkIdAndLanguage($this->cid, $target_language);
     // 2. delete all segment targets associated with given language
     self::deleteSegmentTranslationsByChunkIdAndLanguage($this->cid, $target_language);
     // 3. insert all segments for the given language
     self::saveSegmentTranslations($document_xml, $target_language);
     // 4. return the dirty targets' statuses
     self::restoreDirtyLids($dirty_lids);
     /* END FAST */
     // set chunk status to current
     $this->setStatus(LingotekSync::STATUS_CURRENT);
     $this->setTargetsStatus(LingotekSync::STATUS_CURRENT, $lingotek_locale);
     return TRUE;
 }
Ejemplo n.º 3
0
 public static function getDirtyChunkLids()
 {
     // return the list of all lids from the locale_source table *not* fully translated
     $source_language = language_default();
     if (!isset($source_language->lingotek_locale)) {
         $source_language->lingotek_locale = Lingotek::convertDrupal2Lingotek($source_language->language);
     }
     $lingotek_codes = Lingotek::getLanguagesWithoutSource($source_language->lingotek_locale);
     if (!count($lingotek_codes)) {
         LingotekLog::error('No languages configured for this Lingotek account.', array());
         return array();
     }
     // get the drupal language for each associated lingotek locale
     $drupal_codes = array();
     foreach ($lingotek_codes as $lc) {
         $drupal_codes[] = Lingotek::convertLingotek2Drupal($lc);
     }
     // get the list of all segments that need updating
     // that belong to the textgroups the user wants translated
     $textgroups = array_merge(array(-1), LingotekConfigChunk::getTextgroupsForTranslation());
     $max_length = variable_get('lingotek_config_max_source_length', LINGOTEK_CONFIG_MAX_SOURCE_LENGTH);
     $query = db_select('{locales_source}', 'ls');
     $query->fields('ls', array('lid'))->condition('ls.source', '', '!=')->condition('ls.lid', self::getQueryCompletedConfigTranslations($drupal_codes), 'NOT IN')->where('length(ls.source) < ' . (int) $max_length);
     if (in_array('misc', $textgroups)) {
         $or = db_or();
         $or->condition('ls.textgroup', $textgroups, 'IN');
         $or->where("ls.textgroup NOT IN ('default','menu','taxonomy','views','blocks','field')");
         $query->condition($or);
     } else {
         $query->condition('ls.textgroup', $textgroups, 'IN');
     }
     return $query->execute()->fetchCol();
 }
Ejemplo n.º 4
0
 /**
  * Updates the local content with data from a Lingotek Document.
  *
  * @return bool
  *   TRUE if the content updates succeeded, FALSE otherwise.
  */
 public function updateLocalContent()
 {
     $success = TRUE;
     $metadata = $this->metadata();
     if (!empty($metadata['document_id'])) {
         $document_id = $metadata['document_id'];
         $api = LingotekApi::instance();
         $document = $api->getDocument($document_id);
         foreach ($document->translationTargets as $target) {
             $document_xml = $api->downloadDocument($metadata['document_id'], $target->language);
             $target_language = Lingotek::convertLingotek2Drupal($target->language);
             foreach ($document_xml as $drupal_field_name => $content) {
                 // Figure out which subkey of the field data we're targeting.
                 // "value" for standard text fields, or some other key for
                 // compound text fields (text with summary, for example).
                 $target_key = 'value';
                 $subfield_parts = explode('__', $drupal_field_name);
                 if (count($subfield_parts) == 2) {
                     $drupal_field_name = $subfield_parts[0];
                     $target_key = $subfield_parts[1];
                 }
                 $field = field_info_field($drupal_field_name);
                 if (!empty($field['lingotek_translatable'])) {
                     $comment_field =& $this->comment->{$drupal_field_name};
                     $index = 0;
                     foreach ($content as $text) {
                         $comment_field[$target_language][$index][$target_key] = decode_entities(lingotek_xml_decode($text));
                         // Copy filter format from source language field.
                         if (!empty($comment_field[$this->comment->language][0]['format'])) {
                             $comment_field[$target_language][$index]['format'] = $comment_field[$this->comment->language][0]['format'];
                         }
                         $index++;
                     }
                 }
             }
             $comment_node = LingotekNode::loadById($this->comment->nid);
             $comment_fields = array_keys(field_info_instances('comment', 'comment_node_' . $comment_node->type));
             foreach ($comment_fields as $field) {
                 // Copy any untranslated fields from the default language into this target.
                 if (isset($this->comment->{$field}[$this->comment->language]) && !isset($this->comment->{$field}[$target_language])) {
                     $this->comment->{$field}[$target_language] = $this->comment->{$field}[$this->comment->language];
                 }
                 // Ensure that all fields get their LANGUAGE_NONE field data populated with the
                 // comment's default language data, to support toggling off of comment translation
                 // at some point in the future.
                 if (!empty($this->comment->{$field}[$this->comment->language])) {
                     $this->comment->{$field}[LANGUAGE_NONE] = $this->comment->{$field}[$this->comment->language];
                 }
             }
         }
         // This avoids an infitinite loop when hooks resulting from comment_save() are invoked.
         self::$content_update_in_progress = TRUE;
         comment_save($this->comment);
         self::$content_update_in_progress = FALSE;
         $this->comment = comment_load($this->comment->cid);
     } else {
         LingotekLog::error('Unable to refresh local contents for comment @cid. Could not find Lingotek Document ID.', array('@cid' => $this->comment->cid));
         $success = FALSE;
     }
     return $success;
 }