| [ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
1 <?php 2 class GP_Route_Translation extends GP_Route_Main { 3 function import_translations_get( $project_path, $locale_slug, $translation_set_slug ) { 4 $project = GP::$project->by_path( $project_path ); 5 $locale = GP_Locales::by_slug( $locale_slug ); 6 $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug ); 7 if ( !$project || !$locale || !$translation_set ) gp_tmpl_404(); 8 if ( $this->cannot_and_redirect( 'approve', 'translation-set', $translation_set->id ) ) return; 9 $kind = 'translations'; 10 $this->tmpl( 'project-import', get_defined_vars() ); 11 } 12 13 function import_translations_post( $project_path, $locale_slug, $translation_set_slug ) { 14 $project = GP::$project->by_path( $project_path ); 15 $locale = GP_Locales::by_slug( $locale_slug ); 16 $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug ); 17 if ( !$project || !$locale || !$translation_set ) gp_tmpl_404(); 18 if ( $this->cannot_and_redirect( 'approve', 'translation-set', $translation_set->id ) ) return; 19 20 $format = gp_array_get( GP::$formats, gp_post( 'format', 'po' ), null ); 21 if ( !$format ) { 22 $this->redirect_with_error( __('No such format.') ); 23 return; 24 } 25 26 if ( !is_uploaded_file( $_FILES['import-file']['tmp_name'] ) ) { 27 $this->redirect_with_error( __('Error uploading the file.') ); 28 return; 29 } 30 31 $translations = $format->read_translations_from_file( $_FILES['import-file']['tmp_name'], $project ); 32 if ( !$translations ) { 33 $this->redirect_with_error( __('Couldn’t load translations from file!') ); 34 return; 35 } 36 37 $translations_added = $translation_set->import( $translations ); 38 $this->notices[] = sprintf(__("%s translations were added"), $translations_added ); 39 40 $this->redirect( gp_url_project( $project, gp_url_join( $locale->slug, $translation_set->slug ) ) ); 41 } 42 43 function export_translations_get( $project_path, $locale_slug, $translation_set_slug ) { 44 $project = GP::$project->by_path( $project_path ); 45 $locale = GP_Locales::by_slug( $locale_slug ); 46 $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug ); 47 if ( ! $project || ! $locale || ! $translation_set ) gp_tmpl_404(); 48 49 $format = gp_array_get( GP::$formats, gp_get( 'format', 'po' ), null ); 50 if ( ! $format ) gp_tmpl_404(); 51 52 53 if( method_exists( $format, 'get_filename' ) ) { 54 $filename = call_user_func( array( $format, 'get_filename' ), $project, $locale ); 55 } 56 else { 57 // If format didn't implement it 58 $export_locale = apply_filters( 'export_locale', $locale->slug, $locale, $format ); 59 $filename = sprintf( '%s-%s.' . $format->extension, str_replace( '/', '-', $project->path ), $export_locale ); 60 } 61 62 $entries = GP::$translation->for_export( $project, $translation_set, gp_get( 'filters' ) ); 63 64 if ( gp_has_translation_been_updated( $translation_set ) ) { 65 $this->headers_for_download( $filename, $translation_set ); 66 echo $format->print_exported_file( $project, $locale, $translation_set, $entries ); 67 } 68 else { 69 // As has_translation_been_updated() compared against HTTP_IF_MODIFIED_SINCE here, send an appropriate header. 70 $this->header( 'HTTP/1.1 304 Not Modified' ); 71 } 72 } 73 74 function translations_get( $project_path, $locale_slug, $translation_set_slug ) { 75 $project = GP::$project->by_path( $project_path ); 76 $locale = GP_Locales::by_slug( $locale_slug ); 77 $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug ); 78 if ( !$project || !$locale || !$translation_set ) gp_tmpl_404(); 79 $page = gp_get( 'page', 1 ); 80 $filters = gp_get( 'filters', array() ); 81 $sort = gp_get( 'sort', array() ); 82 if ( 'random' == gp_array_get( $sort, 'by') ) { 83 add_filter( 'gp_pagination', create_function( '$html', 'return "";' ) ); 84 } 85 86 $per_page = GP::$user->current()->get_meta('per_page'); 87 if ( 0 == $per_page ) 88 $per_page = GP::$translation->per_page; 89 else 90 GP::$translation->per_page = $per_page; 91 92 $translations = GP::$translation->for_translation( $project, $translation_set, $page, $filters, $sort ); 93 $total_translations_count = GP::$translation->found_rows; 94 95 $can_edit = GP::$user->logged_in(); 96 $can_write = $this->can( 'write', 'project', $project->id ); 97 $can_approve = $this->can( 'approve', 'translation-set', $translation_set->id ); 98 $url = gp_url_project( $project, gp_url_join( $locale->slug, $translation_set->slug ) ); 99 $set_priority_url = gp_url( '/originals/%original-id%/set_priority'); 100 $discard_warning_url = gp_url_project( $project, gp_url_join( $locale->slug, $translation_set->slug, '-discard-warning' ) ); 101 $set_status_url = gp_url_project( $project, gp_url_join( $locale->slug, $translation_set->slug, '-set-status' ) ); 102 $bulk_action = gp_url_join( $url, '-bulk' ); 103 $this->tmpl( 'translations', get_defined_vars() ); 104 } 105 106 function translations_post ( $project_path, $locale_slug, $translation_set_slug ) { 107 $this->logged_in_or_forbidden(); 108 $project = GP::$project->by_path( $project_path ); 109 $locale = GP_Locales::by_slug( $locale_slug ); 110 $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug ); 111 if ( !$project || !$locale || !$translation_set ) gp_tmpl_404(); 112 113 $output = array(); 114 foreach( gp_post( 'translation', array() ) as $original_id => $translations) { 115 $data = compact('original_id'); 116 $data['user_id'] = GP::$user->current()->id; 117 $data['translation_set_id'] = $translation_set->id; 118 119 foreach( range( 0, GP::$translation->get_static( 'number_of_plural_translations' ) ) as $i ) { 120 if ( isset( $translations[$i] ) ) $data["translation_$i"] = $translations[$i]; 121 } 122 123 if ( $this->can( 'approve', 'translation-set', $translation_set->id ) || $this->can( 'write', 'project', $project->id ) ) 124 $data['status'] = 'current'; 125 else 126 $data['status'] = 'waiting'; 127 128 $original = GP::$original->get( $original_id ); 129 $data['warnings'] = GP::$translation_warnings->check( $original->singular, $original->plural, $translations, $locale ); 130 131 $translation = GP::$translation->create( $data ); 132 if ( ! $translation->validate() ) { 133 $error_output = '<ul>'; 134 foreach ($translation->errors as $error) { 135 $error_output .= '<li>' . $error . '</li>'; 136 } 137 $error_output .= '</ul>'; 138 $translation->delete(); 139 $this->die_with_error( $error_output, 200 ); 140 } 141 else { 142 if ( 'current' == $data['status'] ) 143 $translation->set_status( 'current' ); 144 145 wp_cache_delete( $translation_set->id, 'translation_set_status_breakdown' ); 146 $translations = GP::$translation->for_translation( $project, $translation_set, 'no-limit', array('translation_id' => $translation->id), array() ); 147 148 if ( $translations ) { 149 $t = $translations[0]; 150 $parity = returner( 'even' ); 151 $can_edit = GP::$user->logged_in(); 152 $can_write = $this->can( 'write', 'project', $project->id ); 153 $can_approve = $this->can( 'approve', 'translation-set', $translation_set->id ); 154 $output[$original_id] = gp_tmpl_get_output( 'translation-row', get_defined_vars() ); 155 } 156 else { 157 $output[$original_id] = false; 158 } 159 } 160 } 161 echo json_encode( $output ); 162 } 163 164 function bulk_post( $project_path, $locale_slug, $translation_set_slug ) { 165 166 $project = GP::$project->by_path( $project_path ); 167 $locale = GP_Locales::by_slug( $locale_slug ); 168 $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug ); 169 if ( !$project || !$locale || !$translation_set ) gp_tmpl_404(); 170 if ( $this->cannot_and_redirect( 'approve', 'translation-set', $translation_set->id ) ) return; 171 172 $bulk = gp_post('bulk'); 173 $bulk['row-ids'] = array_filter( explode( ',', $bulk['row-ids'] ) ); 174 if ( !empty( $bulk['row-ids'] ) ) { 175 if ( 'approve' == $bulk['action'] || 'reject' == $bulk['action'] ) { 176 $this->_bulk_approve( $project, $locale, $translation_set, $bulk ); 177 } 178 179 if ( 'gtranslate' == $bulk['action'] ) { 180 $this->_bulk_google_translate( $project, $locale, $translation_set, $bulk ); 181 } 182 } else { 183 $this->errors[] = 'No translations were supplied.'; 184 } 185 186 wp_cache_delete( $translation_set->id, 'translation_set_status_breakdown' ); 187 188 // hack, until we make clean_url() to allow [ and ] 189 $bulk['redirect_to'] = str_replace( array('[', ']'), array_map('urlencode', array('[', ']')), $bulk['redirect_to']); 190 $this->redirect( $bulk['redirect_to'] ); 191 } 192 193 function _bulk_approve( $project, $locale, $translation_set, $bulk ) { 194 195 $action = $bulk['action']; 196 197 $ok = $error = 0; 198 $new_status = 'approve' == $action? 'current' : 'rejected'; 199 foreach( $bulk['row-ids'] as $row_id ) { 200 $translation_id = gp_array_get( split( '-', $row_id ), 1 ); 201 $translation = GP::$translation->get( $translation_id ); 202 if ( !$translation ) continue; 203 if ( $translation->set_status( $new_status ) ) 204 $ok++; 205 else 206 $error++; 207 } 208 209 if ( 0 === $error) { 210 $this->notices[] = 'approve' == $action? 211 sprintf( _n('One translation approved.', '%d translations approved.', $ok), $ok ): 212 sprintf( _n('One translation rejected.', '%d translations rejected.', $ok), $ok ); 213 } else { 214 if ( $ok > 0 ) { 215 $message = 'approve' == $action? 216 sprintf( _n('Error with approving one translation.', 'Error with approving %s translations.', $error), $error ): 217 sprintf( _n('Error with rejecting one translation.', 'Error with rejecting %s translations.', $error), $error ); 218 $message .= ' '; 219 $message .= 'approve' == $action? 220 sprintf( _n( 221 'The remaining translation was approved successfully.', 222 'The remaining %s translations were approved successfully.', $ok), $ok ): 223 sprintf( _n( 224 'The remaining translation was rejected successfully.', 225 'The remaining %s translations were rejected successfully.', $ok), $ok ); 226 $this->errors[] = $message; 227 } else { 228 $this->errors[] = 'approve' == $action? 229 sprintf( _n( 230 'Error with approving the translation.', 231 'Error with approving all %s translation.', $error), $error ): 232 sprintf( _n( 233 'Error with rejecting the translation.', 234 'Error with rejecting all %s translation.', $error), $error ); 235 } 236 } 237 } 238 239 function _bulk_google_translate( $project, $locale, $translation_set, $bulk ) { 240 $google_errors = 0; 241 $insert_errors = 0; 242 $ok = 0; 243 $skipped = 0; 244 245 $singulars = array(); 246 $original_ids = array(); 247 foreach( $bulk['row-ids'] as $row_id ) { 248 if ( gp_in( '-', $row_id) ) { 249 $skipped++; 250 continue; 251 } 252 $original_id = gp_array_get( split( '-', $row_id ), 0 ); 253 $original = GP::$original->get( $original_id ); 254 if ( !$original || $original->plural ) { 255 $skipped++; 256 continue; 257 } 258 $singulars[] = $original->singular; 259 $original_ids[] = $original_id; 260 } 261 $results = google_translate_batch( $locale, $singulars ); 262 if ( is_wp_error( $results ) ) { 263 error_log( print_r( $results, true ) ); 264 $this->errors[] = $results->get_error_message(); 265 return; 266 } 267 foreach( gp_array_zip( $original_ids, $singulars, $results ) as $item ) { 268 list( $original_id, $singular, $translation ) = $item; 269 if ( is_wp_error( $translation ) ) { 270 $google_errors++; 271 error_log( $translation->get_error_message() ); 272 continue; 273 } 274 $data = compact( 'original_id' ); 275 $data['user_id'] = GP::$user->current()->id; 276 $data['translation_set_id'] = $translation_set->id; 277 $data['translation_0'] = $translation; 278 $data['status'] = 'fuzzy'; 279 $data['warnings'] = GP::$translation_warnings->check( $singular, null, array( $translation ), $locale ); 280 $inserted = GP::$translation->create( $data ); 281 $inserted? $ok++ : $insert_errors++; 282 } 283 if ( $google_errors > 0 || $insert_errors > 0 ) { 284 $message = array(); 285 if ( $ok ) $message[] = sprintf( __('Added: %d.' ), $ok ); 286 if ( $google_errors ) $message[] = sprintf( __('Error from Google Translate: %d.' ), $google_errors ); 287 if ( $insert_errors ) $message[] = sprintf( __('Error adding: %d.' ), $insert_errors ); 288 if ( $skipped ) $message[] = sprintf( __('Skipped: %d.' ), $skipped ); 289 $this->errors[] = implode( '', $message ); 290 } else { 291 $this->notices[] = sprintf( __('%d fuzzy translation from Google Translate were added.' ), $ok ); 292 } 293 } 294 295 function discard_warning( $project_path, $locale_slug, $translation_set_slug ) { 296 return $this->edit_single_translation( $project_path, $locale_slug, $translation_set_slug, array( $this, 'discard_warning_edit_function' ) ); 297 } 298 299 function set_status( $project_path, $locale_slug, $translation_set_slug ) { 300 return $this->edit_single_translation( $project_path, $locale_slug, $translation_set_slug, array( $this, 'set_status_edit_function' ) ); 301 } 302 303 private function edit_single_translation( $project_path, $locale_slug, $translation_set_slug, $edit_function ) { 304 $project = GP::$project->by_path( $project_path ); 305 $locale = GP_Locales::by_slug( $locale_slug ); 306 $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug ); 307 if ( !$project || !$locale || !$translation_set ) gp_tmpl_404(); 308 309 $translation = GP::$translation->get( gp_post( 'translation_id' ) ); 310 if ( !$translation ) { 311 $this->die_with_error( 'Translation doesn’t exist!' ); 312 } 313 $this->can_approve_translation_or_forbidden( $translation ); 314 315 call_user_func( $edit_function, $project, $locale, $translation_set, $translation ); 316 317 $translations = GP::$translation->for_translation( $project, $translation_set, 'no-limit', array('translation_id' => $translation->id, 'status' => 'either'), array() ); 318 if ( $translations ) { 319 $t = $translations[0]; 320 $parity = returner( 'even' ); 321 $can_edit = GP::$user->logged_in(); 322 $can_approve = $this->can( 'approve', 'translation-set', $translation_set->id ); 323 $this->tmpl( 'translation-row', get_defined_vars() ); 324 } else { 325 $this->die_with_error( 'Error in retrieving translation!' ); 326 } 327 } 328 329 private function discard_warning_edit_function( $project, $locale, $translation_set, $translation ) { 330 if ( !isset( $translation->warnings[gp_post( 'index' )][gp_post( 'key' )] ) ) { 331 $this->die_with_error( 'The warning doesn’exist!' ); 332 } 333 unset( $translation->warnings[gp_post( 'index' )][gp_post( 'key' )] ); 334 if ( empty( $translation->warnings[gp_post( 'index' )] ) ) { 335 unset( $translation->warnings[gp_post( 'index' )] ); 336 } 337 $res = $translation->save(); 338 if ( !$res ) { 339 $this->die_with_error( 'Error in saving the translation!' ); 340 } 341 342 } 343 344 private function set_status_edit_function( $project, $locale, $translation_set, $translation ) { 345 $res = $translation->set_status( gp_post( 'status' ) ); 346 if ( !$res ) { 347 $this->die_with_error( 'Error in saving the translation status!' ); 348 } 349 } 350 351 private function can_approve_translation_or_forbidden( $translation ) { 352 $can_reject_self = (GP::$user->current()->id == $translation->user_id && $translation->status == "waiting"); 353 if ( $can_reject_self ) { 354 return; 355 } 356 $this->can_or_forbidden( 'approve', 'translation-set', $translation->translation_set_id ); 357 } 358 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue May 21 03:59:56 2013 | Hosted by follow the white rabbit. |