Skip to content

Commit

Permalink
Add filter that enables not marking existing translations as fuzzy on…
Browse files Browse the repository at this point in the history
… similar string import.

Fixes GlotPress#629
  • Loading branch information
yoavf committed Jan 1, 2017
1 parent 0b8708c commit b6a9c24
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
21 changes: 19 additions & 2 deletions gp-includes/things/original.php
Expand Up @@ -280,16 +280,33 @@ public function import_for_project( $project, $translations ) {
if ( $close_original ) {
$original = $originals_by_key[ $close_original ];

/**
* Filters whether to set existing translations to fuzzy.
*
* This filter is called when a new string closely match an existing possibly dropped string.
*
* @since 2.3.0
*
* @param bool $do_fuzzy Whether to set existing translations to fuzzy. Default true.
* @param object $data The new original data.
* @param object $original The previous original being replaced.
*/
$do_fuzzy = apply_filters( 'gp_close_original_fuzzy_translations', true, (object) $data, $original );

// We'll update the old original...
$this->update( $data, array( 'id' => $original->id ) );

// and set existing translations to fuzzy.
$this->set_translations_for_original_to_fuzzy( $original->id );
if ( $do_fuzzy ) {
$this->set_translations_for_original_to_fuzzy( $original->id );
$originals_fuzzied++;
} else {
$originals_existing++;
}

// No need to obsolete it now.
unset( $possibly_dropped[ $close_original ] );

$originals_fuzzied++;
continue;
} else { // Completely new string
$created = GP::$original->create( $data );
Expand Down
25 changes: 25 additions & 0 deletions tests/phpunit/testcases/tests_things/test_thing_original.php
Expand Up @@ -114,6 +114,31 @@ function test_import_should_mark_translation_of_changed_strings_as_fuzzy() {
$this->assertEquals( 1, count( $fuzzy_translations ) );
}

function test_import_should_not_mark_translation_of_changed_strings_as_fuzzy_with_filter() {
$set = $this->factory->translation_set->create_with_project_and_locale();
$original = $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'baba baba' ) );
$translation = $this->factory->translation->create( array( 'translation_set_id' => $set->id, 'original_id' => $original->id, 'status' => 'current' ) );
$translations_for_import = $this->create_translations_with( array( array( 'singular' => 'baba baba.' ) ) );

add_filter( 'gp_close_original_fuzzy_translations', '__return_false' );

list( $originals_added, $originals_existing, $originals_fuzzied, $originals_obsoleted, $originals_error ) = $original->import_for_project( $set->project, $translations_for_import );

remove_filter( 'gp_close_original_fuzzy_translations', '__return_false' );

$this->assertEquals( 0, $originals_added );
$this->assertEquals( 1, $originals_existing );
$this->assertEquals( 0, $originals_fuzzied );
$this->assertEquals( 0, $originals_obsoleted );
$this->assertEquals( 0, $originals_error );

$current_translations = GP::$translation->find_many( "original_id = '{$original->id}' AND status = 'current'" );
$fuzzy_translations = GP::$translation->find_many( "original_id = '{$original->id}' AND status = 'fuzzy'" );

$this->assertEquals( 1, count( $current_translations ) );
$this->assertEquals( 0, count( $fuzzy_translations ) );
}

/**
* @ticket 508
*/
Expand Down

0 comments on commit b6a9c24

Please sign in to comment.