Skip to content

Commit

Permalink
Closes GlotPress#377: import translations as waiting
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Apr 28, 2016
1 parent 99034df commit 4bb0a90
Show file tree
Hide file tree
Showing 6 changed files with 495 additions and 22 deletions.
35 changes: 30 additions & 5 deletions gp-includes/routes/translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public function import_translations_get( $project_path, $locale_slug, $translati
return $this->die_with_404();
}

if ( $this->cannot_and_redirect( 'approve', 'translation-set', $translation_set->id ) ) {
$can_import_current = $this->can( 'approve', 'translation-set', $translation_set->id );
$can_import_waiting = $can_approve || $this->can( 'import-waiting', 'translation-set', $translation_set->id );

if ( ! $can_import_current && ! $can_import_waiting ) {
$this->redirect_with_error( __( 'You are not allowed to do that!', 'glotpress' ) );
return;
}

Expand All @@ -54,7 +58,26 @@ public function import_translations_post( $project_path, $locale_slug, $translat
return $this->die_with_404();
}

if ( $this->cannot_and_redirect( 'approve', 'translation-set', $translation_set->id ) ) {
$can_import_current = $this->can( 'approve', 'translation-set', $translation_set->id );
$can_import_waiting = $can_approve || $this->can( 'import-waiting', 'translation-set', $translation_set->id );

if ( ! $can_import_current && ! $can_import_waiting ) {
$this->redirect_with_error( __( 'You are not allowed to do that!', 'glotpress' ) );
return;
}

$import_status = gp_post( 'status', 'waiting' );

$allowed_import_status = array();
if ( $can_import_current ) {
$allowed_import_status[] = 'current';
}
if ( $can_import_waiting ) {
$allowed_import_status[] = 'waiting';
}

if ( ! in_array( $import_status, $allowed_import_status, true ) ) {
$this->redirect_with_error( __( 'Invalid translation status.', 'glotpress' ) );
return;
}

Expand All @@ -63,20 +86,20 @@ public function import_translations_post( $project_path, $locale_slug, $translat
return;
}

$format = gp_get_import_file_format( gp_post( 'format', 'po' ), $_FILES[ 'import-file' ][ 'name' ] );
$format = gp_get_import_file_format( gp_post( 'format', 'po' ), $_FILES['import-file']['name'] );

if ( ! $format ) {
$this->redirect_with_error( __( 'No such format.', 'glotpress' ) );
return;
}

$translations = $format->read_translations_from_file( $_FILES['import-file']['tmp_name'], $project );
if ( !$translations ) {
if ( ! $translations ) {
$this->redirect_with_error( __( 'Couldn’t load translations from file!', 'glotpress' ) );
return;
}

$translations_added = $translation_set->import( $translations );
$translations_added = $translation_set->import( $translations, $import_status );
$this->notices[] = sprintf( __( '%s translations were added', 'glotpress' ), $translations_added );

$this->redirect( gp_url_project( $project, gp_url_join( $locale->slug, $translation_set->slug ) ) );
Expand Down Expand Up @@ -192,6 +215,8 @@ public function translations_get( $project_path, $locale_slug, $translation_set_
$can_edit = $this->can( 'edit', 'translation-set', $translation_set->id );
$can_write = $this->can( 'write', 'project', $project->id );
$can_approve = $this->can( 'approve', 'translation-set', $translation_set->id );
$can_import_current = $can_approve;
$can_import_waiting = $can_approve || $this->can( 'import-waiting', 'translation-set', $translation_set->id );
$url = gp_url_project( $project, gp_url_join( $locale->slug, $translation_set->slug ) );
$set_priority_url = gp_url( '/originals/%original-id%/set_priority');
$discard_warning_url = gp_url_project( $project, gp_url_join( $locale->slug, $translation_set->slug, '-discard-warning' ) );
Expand Down
68 changes: 54 additions & 14 deletions gp-includes/things/translation-set.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,27 +129,43 @@ public function by_project_id( $project_id ) {
WHERE project_id = %d ORDER BY name ASC", $project_id );
}

public function import( $translations ) {
$this->set_memory_limit('256M');
/**
* Import translations from a Translations object.
*
* @param Translations $translations the translations to be imported to this translation-set.
* @param string $desired_status 'current' or 'waiting'.
* @return boolean or void
*/
public function import( $translations, $desired_status = 'current' ) {
$this->set_memory_limit( '256M' );

if ( !isset( $this->project ) || !$this->project ) $this->project = GP::$project->get( $this->project_id );
if ( ! isset( $this->project ) || ! $this->project ) {
$this->project = GP::$project->get( $this->project_id );
}

if ( ! in_array( $desired_status, array( 'current', 'waiting' ), true ) ) {
return false;
}

$locale = GP_Locales::by_slug( $this->locale );
$user = wp_get_current_user();

$current_translations_list = GP::$translation->for_translation( $this->project, $this, 'no-limit', array('status' => 'current', 'translated' => 'yes') );
$current_translations = new Translations();
foreach( $current_translations_list as $entry ) {
$current_translations->add_entry( $entry );
$existing_translations = array();

$current_translations_list = GP::$translation->for_translation( $this->project, $this, 'no-limit', array( 'status' => 'current', 'translated' => 'yes' ) );
$existing_translations['current'] = new Translations();
foreach ( $current_translations_list as $entry ) {
$existing_translations['current']->add_entry( $entry );
}
unset( $current_translations_list );

$translations_added = 0;
foreach( $translations->entries as $entry ) {
foreach ( $translations->entries as $entry ) {
if ( empty( $entry->translations ) ) {
continue;
}

$is_fuzzy = in_array( 'fuzzy', $entry->flags );
$is_fuzzy = in_array( 'fuzzy', $entry->flags, true );

/**
* Filter whether to import fuzzy translations.
Expand All @@ -164,12 +180,36 @@ public function import( $translations ) {
continue;
}

/**
* Filter the the status of imported translations of a translation set.
*
* @since 1.0.0
*
* @param string $status The status of imported translations.
*/
$entry->status = apply_filters( 'gp_translation_set_import_status', $is_fuzzy ? 'fuzzy' : $desired_status );

// Lazy load other entries.
if ( ! isset( $existing_translations[ $entry->status ] ) ) {
$existing_translations_list = GP::$translation->for_translation( $this->project, $this, 'no-limit', array( 'status' => $entry->status, 'translated' => 'yes' ) );
$existing_translations[ $entry->status ] = new Translations();
foreach ( $existing_translations_list as $_entry ) {
$existing_translations[ $entry->status ]->add_entry( $_entry );
}
unset( $existing_translations_list );
}

$create = false;
if ( $translated = $current_translations->translate_entry( $entry ) ) {
// we have the same string translated
// create a new one if they don't match
$translated = $existing_translations[ $entry->status ]->translate_entry( $entry );
if ( 'current' !== $entry->status && ! $translated ) {
// Don't create an entry if it already exists as current.
$translated = $existing_translations['current']->translate_entry( $entry );
}

if ( $translated ) {
// We have the same string translated, so create a new one if they don't match.
$entry->original_id = $translated->original_id;
$translated_is_different = array_pad( $entry->translations, $locale->nplurals, null ) != $translated->translations;
$translated_is_different = array_pad( $entry->translations, $locale->nplurals, null ) !== $translated->translations;

/**
* Filter whether to import over an existing translation on a translation set.
Expand All @@ -192,7 +232,7 @@ public function import( $translations ) {
$entry->user_id = $user->ID;
}

$entry->status = $is_fuzzy ? 'fuzzy' : 'current';
$entry->status = $is_fuzzy ? 'fuzzy' : $desired_status;

$entry->warnings = maybe_unserialize( GP::$translation_warnings->check( $entry->singular, $entry->plural, $entry->translations, $locale ) );
if ( ! empty( $entry->warnings ) ) {
Expand Down
27 changes: 25 additions & 2 deletions gp-templates/project-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
if ( 'originals' == $kind ) {
$title = sprintf( __( 'Import Originals < %s < GlotPress', 'glotpress' ), esc_html( $project->name ) );
$return_link = gp_url_project( $project );
gp_breadcrumb_project( $project );
} else {
$title = sprintf( __( 'Import Translations < %s < GlotPress', 'glotpress' ), esc_html( $project->name ) );
$return_link = gp_url_project_locale( $project, $locale->slug, $translation_set->slug );
gp_breadcrumb( array(
gp_project_links_from_root( $project ),
gp_link_get( $return_link, $translation_set->name ),
) );
}

gp_title( $title );
gp_breadcrumb_project( $project );
gp_tmpl_header();
?>

Expand All @@ -24,9 +28,28 @@
$format_options[$slug] = $format->name;
}
$format_dropdown = gp_select( 'format', $format_options, 'auto' );

$status_options = array();
if ( isset( $can_import_current ) && $can_import_current ) {
$status_options['current'] = __( 'Current', 'glotpress' );
}
if ( isset( $can_import_waiting ) && $can_import_waiting ) {
$status_options['waiting'] = __( 'Waiting', 'glotpress' );
}
?>
<dt><label for="format"><?php _e( 'Format:', 'glotpress' ); ?></label></dt>
<dt><label for="format"><?php _e( 'Format:', 'glotpress' ); ?></label></dt>
<dd><?php echo $format_dropdown; ?></dd>
<?php if ( ! empty( $status_options ) ) : ?>
<dt><label for="status"><?php _e( 'Status:', 'glotpress' ); ?></label></dt>
<dd>
<?php if ( count( $status_options ) === 1 ) : ?>
<input type="hidden" name="status" value="<?php echo esc_attr( reset( array_keys( $status_options ) ) ); ?>" />
<?php echo esc_html( reset( array_values( $status_options ) ) ); ?>
<?php elseif ( count( $status_options ) > 1 ) : ?>
<?php echo gp_select( 'status', $status_options, 'waiting' ); ?>
<?php endif; ?>
</dd>
<?php endif; ?>
<dt>
<p>
<input type="submit" name="submit" value="<?php esc_attr_e( 'Import', 'glotpress' ); ?>" id="submit" />
Expand Down
2 changes: 1 addition & 1 deletion gp-templates/translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
<p class="clear actionlist secondary">
<?php
$footer_links = array();
if ( $can_approve ) {
if ( ( isset( $can_import_current ) && $can_import_current ) || ( isset( $can_import_waiting ) && $can_import_waiting ) ) {
$footer_links[] = gp_link_get( gp_url_project( $project, array( $locale->slug, $translation_set->slug, 'import-translations' ) ), __( 'Import translations', 'glotpress' ) );
}
$export_url = gp_url_project( $project, array( $locale->slug, $translation_set->slug, 'export-translations' ) );
Expand Down
Loading

0 comments on commit 4bb0a90

Please sign in to comment.