Skip to content

Commit

Permalink
Merge branch 'glossary-placeholders' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-mendonca committed May 4, 2023
2 parents 36eb5ee + dd4bc69 commit f03c00b
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 6 deletions.
15 changes: 12 additions & 3 deletions assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1285,9 +1285,18 @@ form.filters-toolbar a.revealing.open {
margin-left: 2px;
}

.invisible-spaces {
background-color: var( --gp-color-secondary-600 );
opacity: 0.4;
.invisible-spaces,
.notranslate.placeholder {
background-color: var( --gp-color-neutral-muted );
display: inline-block;
line-height: 1em;
padding: 0.125em;
border-radius: 0.125em;
border: 1px solid var( --gp-color-border-default );
}

.notranslate.placeholder {
font-family: monospace;
}

.ternary {
Expand Down
6 changes: 3 additions & 3 deletions gp-templates/helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function( $m ) use ( &$glossary_entries ) {
// Break out & back into notranslate for translatable attributes.
$text = preg_replace( '/(title|aria-label)=([\'"])([^\\2]+?)\\2/', '\\1=\\2</span>\\3<span class="notranslate">\\2', $text );
// Wrap placeholders with notranslate.
$text = preg_replace( '/(%(\d+\$(?:\d+)?)?[bcdefgosuxEFGX])/', '<span class="notranslate">\\1</span>', $text );
$text = preg_replace( '/(%(\d+\$(?:\d+)?)?[bcdefglosuxEFGX])/', '<span class="notranslate placeholder">\\1</span>', $text );

// Put the glossaries back!
$text = preg_replace_callback(
Expand Down Expand Up @@ -178,7 +178,7 @@ function map_glossary_entries_to_translation_originals( $translation, $glossary
$glossary_entries_reference[ $term ][] = $id;
}

$terms_search = '\b(';
$terms_search = '(';
foreach ( $glossary_entries_suffixes as $term => $suffixes ) {
$terms_search .= preg_quote( $term, '/' );

Expand Down Expand Up @@ -214,7 +214,7 @@ function map_glossary_entries_to_translation_originals( $translation, $glossary

// Remove the trailing |.
$terms_search = substr( $terms_search, 0, -1 );
$terms_search .= ')\b';
$terms_search .= ')';
}

// Split the singular string on glossary terms boundaries.
Expand Down
103 changes: 103 additions & 0 deletions tests/phpunit/testcases/test_template_helper_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,107 @@ function test_prepare_original_with_line_breaks_and_tabs() {
$this->assertEquals( $orig, $expected_result );
}

/**
* Expects matching glossary entries preceded and succeded by placeholders.
*/
function test_map_glossary_entries_to_translation_originals_with_entries_bounded_by_placeholders() {
$test_strings = array(
// Test with simple placeholders before and after glossary matches.
'Words with %ssimple %sstring%s placeholder%s.', // Test %s.
'Words with %dsimple %dinteger%d placeholder%d.', // Test %d.
'Words with %lsimple %llist%l placeholder%l.', // Test %l.
'Words with %1ssimple %1sstring%1d placeholder%1l with minimum width.', // Test %1s, %1d and %1l.
// Test with numbered placeholders before and after glossary matches.
'Words with %1$snumbered %2$sstring%3$s placeholder%4$s.', // Test %s.
'Words with %1$dnumbered %2$dinteger%3$d placeholder%4$d.', // Test %d.
'Words with %1$lnumbered %2$llist%3$l placeholder%4$l.', // Test %l.
'Words with %1$1ssnumbered %2$1sstring%3$1d placeholder%4$1l with minimum width.', // Test %1$1s, %1$1d and %1$1l.
);
$test_string = implode( ' ', $test_strings );

$part_of_speech = 'noun';

$matches = array(
'string' => array( // Portuguese.
'string', // Singular.
),
'número inteiro' => array( // Portuguese.
'integer', // Singular.
),
'lista' => array( // Portuguese.
'list', // Singular.
),
'simples' => array( // Portuguese.
'simple', // Singular.
),
'numerado' => array( // Portuguese.
'numbered', // Singular.
),
'variável' => array( // Portuguese.
'placeholder', // Singular.
),
);

$expected_results = array(
// Test with simple placeholders before and after glossary matches.
'Words with <span class="notranslate placeholder">%s</span><span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;simples&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">simple</span> <span class="notranslate placeholder">%s</span><span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;string&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">string</span><span class="notranslate placeholder">%s</span> <span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;vari&aacute;vel&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">placeholder</span><span class="notranslate placeholder">%s</span>.',
'Words with <span class="notranslate placeholder">%d</span><span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;simples&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">simple</span> <span class="notranslate placeholder">%d</span><span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;n&uacute;mero inteiro&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">integer</span><span class="notranslate placeholder">%d</span> <span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;vari&aacute;vel&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">placeholder</span><span class="notranslate placeholder">%d</span>.',
'Words with <span class="notranslate placeholder">%l</span><span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;simples&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">simple</span> <span class="notranslate placeholder">%l</span>list<span class="notranslate placeholder">%l</span> <span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;vari&aacute;vel&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">placeholder</span><span class="notranslate placeholder">%l</span>.',
'Words with <span class="notranslate placeholder">%1s</span><span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;simples&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">simple</span> <span class="notranslate placeholder">%1s</span><span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;string&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">string</span><span class="notranslate placeholder">%1d</span> <span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;vari&aacute;vel&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">placeholder</span><span class="notranslate placeholder">%1l</span> with minimum width.',
// Test with numbered placeholders before and after glossary matches.
'Words with <span class="notranslate placeholder">%1$s</span><span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;numerado&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">numbered</span> <span class="notranslate placeholder">%2$s</span><span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;string&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">string</span><span class="notranslate placeholder">%3$s</span> placeholder<span class="notranslate placeholder">%4$s</span>.',
'Words with <span class="notranslate placeholder">%1$d</span><span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;numerado&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">numbered</span> <span class="notranslate placeholder">%2$d</span><span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;n&uacute;mero inteiro&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">integer</span><span class="notranslate placeholder">%3$d</span> placeholder<span class="notranslate placeholder">%4$d</span>.',
'Words with <span class="notranslate placeholder">%1$l</span><span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;numerado&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">numbered</span> <span class="notranslate placeholder">%2$l</span>list<span class="notranslate placeholder">%3$l</span> placeholder<span class="notranslate placeholder">%4$l</span>.',
'Words with <span class="notranslate placeholder">%1$1s</span><span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;numerado&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">numbered</span> <span class="notranslate placeholder">%2$1s</span><span class="glossary-word" data-translations="[{&quot;translation&quot;:&quot;string&quot;,&quot;pos&quot;:&quot;' . $part_of_speech . '&quot;,&quot;comment&quot;:null,&quot;locale_entry&quot;:&quot;&quot;}]">string</span><span class="notranslate placeholder">%3$1d</span> placeholder<span class="notranslate placeholder">%4$1l</span> with minimum width.',
);

$expected_result = implode( ' ', $expected_results );

$entry = new Translation_Entry( array( 'singular' => $test_string, ) );

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

$glossary_entries = array(
array(
'term' => 'string',
'part_of_speech' => $part_of_speech,
'translation' => 'string', // Portuguese.
'glossary_id' => $glossary->id,
),
array(
'term' => 'integer',
'part_of_speech' => $part_of_speech,
'translation' => 'número inteiro', // Portuguese.
'glossary_id' => $glossary->id,
),
array(
'term' => 'list',
'part_of_speech' => $part_of_speech,
'translation' => 'lista', // Portuguese.
'glossary_id' => $glossary->id,
),
array(
'term' => 'simple',
'part_of_speech' => $part_of_speech,
'translation' => 'simples', // Portuguese.
'glossary_id' => $glossary->id,
),
array(
'term' => 'placeholder',
'part_of_speech' => $part_of_speech,
'translation' => 'variável', // Portuguese.
'glossary_id' => $glossary->id,
),
);

foreach ( $glossary_entries as $glossary_entry ) {
GP::$glossary_entry->create_and_select( $glossary_entry );
}

$orig = map_glossary_entries_to_translation_originals( $entry, $glossary );

$this->assertEquals( $orig->singular_glossary_markup, $expected_result );
}

}

0 comments on commit f03c00b

Please sign in to comment.