Skip to content

Commit

Permalink
Fix, improve and test glossary plural handling
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Oct 21, 2016
1 parent 559842b commit 780469e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
25 changes: 21 additions & 4 deletions gp-templates/helper-functions.php
Expand Up @@ -66,20 +66,37 @@ function map_glossary_entries_to_translations_originals( $translations, $glossar
$matching_entries = array();

foreach( $glossary_entries_terms as $i => $term ) {
$glossary_entry = $glossary_entries[ $i ];
$terms = array( $term );
$terms[] = preg_quote( $term, '/' ) . 's';
if ( 'y' === substr( $term, -1 ) ) {
$terms[] = preg_quote( substr( $term, 0, -1 ), '/' ) . 'ies';
} elseif ( 'f' === substr( $term, -1 ) ) {
$terms[] = preg_quote( substr( $term, 0, -1 ), '/' ) . 'ves';
} elseif ( 'fe' === substr( $term, -2 ) ) {
$terms[] = preg_quote( substr( $term, 0, -2 ), '/' ) . 'ves';
} else{
if ( 'an' === substr( $term, -2 ) ) {
$terms[] = preg_quote( substr( $term, 0, -2 ), '/' ) . 'en';
}
$terms[] = preg_quote( $term, '/' ) . 'es';
}

if ( gp_stripos( $t->singular . ' ' . $t->plural, $term ) !== false ) {
$glossary_entry = $glossary_entries[ $i ];
if ( preg_match( '/\b(' . implode( '|' , $terms ) . ')\b/', $t->singular . ' ' . $t->plural, $m ) ) {
$term = $m[1];
$matching_entries[$term][] = array( 'translation' => $glossary_entry->translation, 'pos' => $glossary_entry->part_of_speech, 'comment' => $glossary_entry->comment );
}
}

//Replace terms in strings with markup
foreach( $matching_entries as $term => $glossary_data ) {
$replacement = '<span class="glossary-word" data-translations="' . htmlspecialchars( wp_json_encode( $glossary_data ), ENT_QUOTES, 'UTF-8') . '">$1</span>';
$translations[$key]->singular_glossary_markup = preg_replace( '/\b(' . preg_quote( $term, '/' ) . '[es|s]?)(?![^<]*<\/span>)\b/iu', $replacement, $translations[$key]->singular_glossary_markup );

$regex = '/\b(' . preg_quote( $term, '/' ) . ')(?![^<]*<\/span>)\b/iu';
$translations[$key]->singular_glossary_markup = preg_replace( $regex, $replacement, $translations[$key]->singular_glossary_markup );

if ( $t->plural ) {
$translations[$key]->plural_glossary_markup = preg_replace( '/\b(' . preg_quote( $term, '/' ) . '[es|s]?)(?![^<]*<\/span>)\b/iu', $replacement, $translations[$key]->plural_glossary_markup );
$translations[$key]->plural_glossary_markup = preg_replace( $regex, $replacement, $translations[$key]->plural_glossary_markup );
}
}
}
Expand Down
57 changes: 53 additions & 4 deletions tests/phpunit/testcases/tests_things/test_thing_glossary_entry.php
Expand Up @@ -49,18 +49,67 @@ function test_part_of_speech_array_set() {
$this->assertCount( 9, GP::$glossary_entry->parts_of_speech );
$this->assertArrayHasKey( 'noun', GP::$glossary_entry->parts_of_speech );
}

function test_delete() {
$entry = GP::$glossary_entry->create( array( 'glossary_id' => '1', 'term' => 'term', 'part_of_speech' => 'verb', 'last_edited_by' =>'1' ) );

$pre_delete = GP::$glossary_entry->find_one( array( 'id' => $entry->id ) );

$entry->delete();

$post_delete = GP::$glossary_entry->find_one( array( 'id' => $entry->id ) );

$this->assertFalse( empty( $pre_delete ) );
$this->assertNotEquals( $pre_delete, $post_delete );
}

}
function test_mapping_entries_to_originals() {
require_once GP_TMPL_PATH . 'helper-functions.php';

$set = $this->factory->translation_set->create_with_project_and_locale();
$glossary = GP::$glossary->create_and_select( array( 'translation_set_id' => $set->id ) );

foreach ( array( 'term', 'box', 'city', 'toy', 'wife', 'shelf', 'man', 'woman' ) as $noun ) {
GP::$glossary_entry->create( array( 'glossary_id' => $glossary->id, 'term' => $noun, 'part_of_speech' => 'noun', 'translation' => $noun, 'comment' => 'my comment', 'last_edited_by' =>'1' ) );
}
foreach ( array( 'post' ) as $verb ) {
GP::$glossary_entry->create( array( 'glossary_id' => $glossary->id, 'term' => $verb, 'part_of_speech' => 'verb', 'translation' => $verb, 'comment' => 'my comment', 'last_edited_by' =>'1' ) );
}

$originals = array(
'term' => array( 'term' ),
'terms' => array( 'term' ),
'A sentence with a term to be found.' => array( 'term' ),
'A sentence with some terms to be found.' => array( 'term' ),
'A sentence with just a box.' => array( 'box' ),
'A sentence that contains a few boxes.' => array( 'box' ),
'A sentence about a city with some boxes.' => array( 'city', 'box' ),
'A blog about a city.' => array( 'city' ),
'Two blogs about two cities.' => array( 'city' ),
'A blog about a toy.' => array( 'toy' ),
'Two blogs about two toys.' => array( 'toy' ),
'A blog about a shelf.' => array( 'shelf' ),
'Two blogs about two shelves.' => array( 'shelf' ),
'A blog about a wife.' => array( 'wife' ),
'Two blogs about two wives.' => array( 'wife' ),
'A blog about a man and a woman.' => array( 'man', 'woman' ),
'Two blogs about two men and two women.' => array( 'man', 'woman' ),
'I post about something.' => array( 'post' ),
'Someone posts something.' => array( 'post' ),
);

foreach ( $originals as $original => $terms ) {
$this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => $original, 'plural' => $original, ) );
}

$translations = GP::$translation->for_translation( $set->project, $set, 'no-limit' );
$translations = map_glossary_entries_to_translations_originals( $translations, $glossary );

foreach ( $translations as $translation ) {
foreach ( $originals[ $translation->singular ] as $term ) {
$this->assertRegExp( '#<span class="glossary-word" data-translations="\[\{&quot;translation&quot;:&quot;' . preg_quote( $term, '#' ) . '&quot;[^"]+">[^<]+</span>#', $translation->singular_glossary_markup, 'Glossary term "' . $term . '" should have been found in "' . $translation->singular . '".' );
$this->assertRegExp( '#<span class="glossary-word" data-translations="\[\{&quot;translation&quot;:&quot;' . preg_quote( $term, '#' ) . '&quot;[^"]+">[^<]+</span>#', $translation->plural_glossary_markup, 'Glossary term "' . $term . '" should have been found in "' . $translation->plural . '".' );
}
}
}
}

0 comments on commit 780469e

Please sign in to comment.