[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/things/ -> translation-set.php (source)

   1  <?php
   2  class GP_Translation_Set extends GP_Thing {
   3  
   4      var $table_basename = 'translation_sets';
   5      var $field_names = array( 'id', 'name', 'slug', 'project_id', 'locale' );
   6      var $non_db_field_names = array( 'wp_locale', 'current_count', 'untranslated_count', 'waiting_count' ); 
   7      var $non_updatable_attributes = array( 'id' );
   8  
   9  	function __construct( $fields = array() ) {
  10          parent::__construct( $fields );
  11  
  12          $locale = GP_Locales::by_slug( $this->locale );
  13  
  14          if( $locale )
  15              $this->wp_locale = $locale->wp_locale;
  16      }
  17  
  18  	function restrict_fields( $set ) {
  19          $set->name_should_not_be('empty');
  20          $set->slug_should_not_be('empty');
  21          $set->locale_should_not_be('empty');
  22          $set->project_id_should_not_be('empty');
  23      }
  24  
  25  	function name_with_locale( $separator = '&rarr;') {
  26          $locale = GP_Locales::by_slug( $this->locale );
  27          $parts = array( $locale->english_name );
  28          if ( 'default' != $this->slug ) $parts[] = $this->name;
  29          return implode( '&nbsp;'.$separator.'&nbsp;', $parts );
  30      }
  31  
  32  	function by_project_id_slug_and_locale( $project_id, $slug, $locale_slug ) {
  33          return $this->one( "
  34              SELECT * FROM $this->table
  35              WHERE slug = '%s' AND project_id= %d AND locale = %s", $slug, $project_id, $locale_slug );
  36      }
  37  
  38  	function by_project_id( $project_id ) {
  39          return $this->many( "
  40              SELECT * FROM $this->table
  41              WHERE project_id = %d ORDER BY name ASC", $project_id );
  42      }
  43  
  44  	function import( $translations ) {
  45          @ini_set('memory_limit', '256M');
  46          if ( !isset( $this->project ) || !$this->project ) $this->project = GP::$project->get( $this->project_id );
  47          $locale = GP_Locales::by_slug( $this->locale );
  48  
  49          $current_translations_list = GP::$translation->for_translation( $this->project, $this, 'no-limit', array('status' => 'current', 'translated' => 'yes') );
  50          $current_translations = new Translations();
  51          foreach( $current_translations_list as $entry ) {
  52              $current_translations->add_entry( $entry );
  53          }
  54          unset( $current_translations_list );
  55          $translations_added = 0;
  56          foreach( $translations->entries as $entry ) {
  57              if ( empty( $entry->translations ) ) continue;
  58              if ( in_array( 'fuzzy', $entry->flags ) ) continue;
  59  
  60              $create = false;
  61  
  62              if ( $translated = $current_translations->translate_entry( $entry ) ) {
  63                  // we have the same string translated
  64                  // create a new one if they don't match
  65                  $entry->original_id = $translated->original_id;
  66                  $create  = ( array_pad( $entry->translations, $locale->nplurals, null ) != $translated->translations );
  67              } else {
  68                  // we don't have the string translated, let's see if the original is there
  69                  $original = GP::$original->by_project_id_and_entry( $this->project->id, $entry, '+active' );
  70                  if ( $original ) {
  71                      $entry->original_id = $original->id;
  72                      $create = true;
  73                  }
  74              }
  75              if ( $create ) {
  76                  $entry->translation_set_id = $this->id;
  77                  $entry->status = 'current';
  78                  // check for errors
  79                  $translation = GP::$translation->create( $entry );
  80                  $translation->set_status( 'current' );
  81                  $translations_added += 1;
  82              }
  83          }
  84          wp_cache_delete( $this->id, 'translation_set_status_breakdown' );
  85          return $translations_added;
  86      }
  87  
  88  	function waiting_count() {
  89          if ( !isset( $this->waiting_count ) ) $this->update_status_breakdown();
  90          return $this->waiting_count;
  91      }
  92  
  93  	function untranslated_count() {
  94          if ( !isset( $this->untranslated_count ) ) $this->update_status_breakdown();
  95          return $this->untranslated_count;
  96      }
  97  
  98  	function current_count() {
  99          if ( !isset( $this->current_count ) ) $this->update_status_breakdown();
 100          return $this->current_count;
 101      }
 102  
 103  	function warnings_count() {
 104          if ( !isset( $this->warnings_count ) ) $this->update_status_breakdown();
 105          return $this->warnings_count;
 106      }
 107  
 108  	function all_count() {
 109          if ( !isset( $this->all_count ) ) $this->all_count  = GP::$original->count_by_project_id( $this->project_id );
 110          return $this->all_count;
 111      }
 112  
 113  
 114  	function update_status_breakdown() {
 115          $counts = wp_cache_get( $this->id, 'translation_set_status_breakdown' );
 116          if ( !is_array( $counts ) ) {
 117              /*
 118               * TODO:
 119               *  - calculate weighted coefficient by priority to know how much of the strings are translated
 120               *     - calculate untranslated
 121               */
 122              $t = GP::$translation->table;
 123              $o = GP::$original->table;
 124              $counts = GP::$translation->many_no_map("
 125                  SELECT t.status as translation_status, COUNT(*) as n
 126                  FROM $t AS t INNER JOIN $o AS o ON t.original_id = o.id WHERE t.translation_set_id = %d AND o.status LIKE '+%%' GROUP BY t.status", $this->id);
 127              $warnings_count = GP::$translation->value_no_map("
 128                  SELECT COUNT(*) FROM $t AS t INNER JOIN $o AS o ON t.original_id = o.id
 129                  WHERE t.translation_set_id = %d AND o.status LIKE '+%%' AND (t.status = 'current' OR t.status = 'waiting') AND warnings IS NOT NULL", $this->id);
 130              $counts[] = (object)array( 'translation_status' => 'warnings', 'n' => $warnings_count );
 131              $counts[] = (object)array( 'translation_status' => 'all', 'n' => $this->all_count() );
 132              wp_cache_set( $this->id, $counts, 'translation_set_status_breakdown' );
 133          }
 134          $statuses = GP::$translation->get_static( 'statuses' );
 135          $statuses[] = 'warnings';
 136          $statuses[] = 'all';
 137          foreach( $statuses as $status ) {
 138              $this->{$status.'_count'} = 0;
 139          }
 140          $this->untranslated_count = 0;
 141          foreach( $counts as $count ) {
 142              if ( in_array( $count->translation_status, $statuses ) ) {
 143                  $this->{$count->translation_status.'_count'} = $count->n;
 144              }
 145          }
 146          $this->untranslated_count = $this->all_count() - $this->current_count;
 147      }
 148  
 149      /**
 150       * Copies translations from a translation set to the current one
 151       *
 152       * This function doesn't merge then, just copies unconditionally. If a translation already exists, it will be duplicated.
 153       */
 154  	function copy_translations_from( $source_translation_set_id ) {
 155          global $gpdb;
 156          $current_date = $this->now_in_mysql_format();
 157          return $this->query("
 158              INSERT INTO $gpdb->translations (
 159                  original_id,       translation_set_id, translation_0, translation_1, translation_2, user_id, status, date_added,       date_modified, warnings
 160              )
 161              SELECT
 162                  original_id, %s AS translation_set_id, translation_0, translation_1, translation_2, user_id, status, date_added, %s AS date_modified, warnings
 163              FROM $gpdb->translations WHERE translation_set_id = %s", $this->id, $current_date, $source_translation_set_id
 164          );
 165      }
 166  
 167  	function percent_translated() {
 168          $original_count = GP::$original->count_by_project_id( $this->project_id );
 169          return sprintf( _x( '%d%%', 'language translation percent' ), $original_count ? $this->current_count() / $original_count * 100 : 0 );
 170      }
 171  }
 172  GP::$translation_set = new GP_Translation_Set();


Generated: Thu May 23 03:59:56 2013 Hosted by follow the white rabbit.