Search texts and states, help tooltip, wider field (F4)
materio_sapi: result messages with real plurals; without any exact match the results are called close (the neighbour search always finds some). Former help sentence of the base page kept, French corrected. Messages in SearchMessage, unit tested; French added by update 11002. materiotheme: placeholder "Describe the material you are looking for", wider search field, "?" button with a help tooltip and clickable examples, "Loading…", "End of results"; texts in vuejs/i18n/search.js. Page title "Base" restored without a search (keys is an array, test its length). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Drupal\Component\Gettext\PoItem;
|
||||
|
||||
/**
|
||||
* Source of the search message (H6) and its French translation.
|
||||
*
|
||||
@@ -42,3 +44,52 @@ function materio_sapi_update_11001(): string {
|
||||
_locale_refresh_translations(['fr'], [$source->lid]);
|
||||
return 'French translation of the search message added.';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sources of the search messages of F4 and their French translation.
|
||||
*
|
||||
* A plural is stored as singular and plural joined by PoItem::DELIMITER, for
|
||||
* the source and for the translation.
|
||||
*/
|
||||
function materio_sapi_search_messages_strings(): array {
|
||||
$plural = fn (string $one, string $many): string => $one . PoItem::DELIMITER . $many;
|
||||
return [
|
||||
'No results for “@keys”. Try other words or a broader description.'
|
||||
=> 'Aucun résultat pour « @keys ». Essayez d’autres mots ou une description plus générale.',
|
||||
$plural('No exact result, @n close result for “@keys”', 'No exact result, @n close results for “@keys”')
|
||||
=> $plural('Aucun résultat exact, @n résultat proche pour « @keys »', 'Aucun résultat exact, @n résultats proches pour « @keys »'),
|
||||
'1 exact result for “@keys”'
|
||||
=> '1 résultat exact pour « @keys »',
|
||||
$plural('@n exact result out of @total relevant results for “@keys”', '@n exact results out of @total relevant results for “@keys”')
|
||||
=> $plural('@n résultat exact sur @total résultats pertinents pour « @keys »', '@n résultats exacts sur @total résultats pertinents pour « @keys »'),
|
||||
// Former text, French corrected ("le champs").
|
||||
'Please use the search form to search from our @count materials.'
|
||||
=> 'Utilisez le champ de recherche pour naviguer dans nos @count matériaux référencés.',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the French translation of the search messages (F4).
|
||||
*/
|
||||
function materio_sapi_update_11002(): string {
|
||||
if (!\Drupal::moduleHandler()->moduleExists('locale')) {
|
||||
return 'locale is off: nothing to translate.';
|
||||
}
|
||||
$storage = \Drupal::service('locale.storage');
|
||||
$lids = [];
|
||||
foreach (materio_sapi_search_messages_strings() as $source_text => $fr) {
|
||||
$source = $storage->findString(['source' => $source_text, 'context' => '']);
|
||||
if (!$source) {
|
||||
$source = $storage->createString(['source' => $source_text, 'context' => ''])->save();
|
||||
}
|
||||
$storage->createTranslation([
|
||||
'lid' => $source->lid,
|
||||
'language' => 'fr',
|
||||
'translation' => $fr,
|
||||
'customized' => LOCALE_CUSTOMIZED,
|
||||
])->save();
|
||||
$lids[] = $source->lid;
|
||||
}
|
||||
_locale_refresh_translations(['fr'], $lids);
|
||||
return 'French translation of the search messages added.';
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ use Drupal\materio_ai\Exception\ModelPausedException;
|
||||
use Drupal\materio_sapi\KeywordText;
|
||||
use Drupal\materio_sapi\RankFusion;
|
||||
use Drupal\materio_sapi\ScoreCut;
|
||||
use Drupal\materio_sapi\SearchMessage;
|
||||
use Drupal\search_api\Entity\Index;
|
||||
|
||||
// https://drupal.stackexchange.com/questions/225008/programatically-use-search-api
|
||||
@@ -484,13 +485,10 @@ class Base extends ControllerBase {
|
||||
$resp['count'] = count($this->results['nids']);
|
||||
$resp['exactematch_count'] = $this->exactematch_count;
|
||||
// Relevant: exact matches and neighbours that stand out (H6); the
|
||||
// count no longer depends on an arbitrary limit. French text added by
|
||||
// materio_sapi_update_11001().
|
||||
$resp['infos'] = t('@exactmatchcount exact result(s) out of @count relevant result(s) for “@keys”', [
|
||||
'@exactmatchcount' => $resp['exactematch_count'],
|
||||
'@count' => $resp['count'],
|
||||
'@keys' => $this->phrase,
|
||||
]);
|
||||
// count no longer depends on an arbitrary limit. Plurals and "close"
|
||||
// results without any exact match (F4).
|
||||
$resp['infos'] = (new SearchMessage($this->getStringTranslation()))
|
||||
->results($resp['exactematch_count'], $resp['count'], $this->phrase, $lang);
|
||||
// $resp['options'] = $this->query->getOptions();
|
||||
|
||||
// $uuids = [];
|
||||
@@ -525,9 +523,10 @@ class Base extends ControllerBase {
|
||||
// $resp['uuids'] = $uuids;
|
||||
$resp['nids'] = $nids;
|
||||
$resp['count'] = $this->count;
|
||||
$resp['infos'] = t('Please use the search form to search from our @count materials.', array(
|
||||
"@count" => $resp['count']
|
||||
));
|
||||
// Help of the page before any search (F4).
|
||||
$lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
||||
$resp['infos'] = (new SearchMessage($this->getStringTranslation()))
|
||||
->help((int) $resp['count'], $lang);
|
||||
}
|
||||
|
||||
return new JsonResponse($resp);
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\materio_sapi;
|
||||
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
use Drupal\Core\StringTranslation\TranslationInterface;
|
||||
|
||||
/**
|
||||
* Messages shown above the search results (F4).
|
||||
*
|
||||
* Real plurals instead of "result(s)". Without any exact match, the other
|
||||
* results are called close, not relevant: the neighbour search always finds
|
||||
* some, even for a word that means nothing.
|
||||
*
|
||||
* Numbers go in @n and @total, formatted for the language; @count is kept by
|
||||
* formatPlural() to choose the plural form only (help() is no plural).
|
||||
*
|
||||
* French texts added by materio_sapi_update_11002().
|
||||
*/
|
||||
final class SearchMessage {
|
||||
|
||||
public function __construct(
|
||||
private readonly TranslationInterface $translation,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Message of a search: exact matches out of all the results shown.
|
||||
*/
|
||||
public function results(int $exact, int $total, string $keys, string $lang): TranslatableMarkup {
|
||||
$args = [
|
||||
'@n' => self::number($exact, $lang),
|
||||
'@total' => self::number($total, $lang),
|
||||
'@keys' => $keys,
|
||||
];
|
||||
if ($total === 0) {
|
||||
return $this->translation->translate('No results for “@keys”. Try other words or a broader description.', $args);
|
||||
}
|
||||
if ($exact === 0) {
|
||||
$args['@n'] = $args['@total'];
|
||||
return $this->translation->formatPlural($total, 'No exact result, @n close result for “@keys”', 'No exact result, @n close results for “@keys”', $args);
|
||||
}
|
||||
if ($total === 1) {
|
||||
return $this->translation->translate('1 exact result for “@keys”', $args);
|
||||
}
|
||||
return $this->translation->formatPlural($exact, '@n exact result out of @total relevant results for “@keys”', '@n exact results out of @total relevant results for “@keys”', $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Message of the search page before any search.
|
||||
*
|
||||
* The former text, kept; its French translation is corrected by
|
||||
* materio_sapi_update_11002() ("le champ").
|
||||
*/
|
||||
public function help(int $materials, string $lang): TranslatableMarkup {
|
||||
return $this->translation->translate('Please use the search form to search from our @count materials.', [
|
||||
'@count' => self::number($materials, $lang),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* A number with the thousands separator of the language.
|
||||
*
|
||||
* French: narrow no-break space (8 073); otherwise a comma (8,073).
|
||||
*/
|
||||
public static function number(int $value, string $lang): string {
|
||||
return number_format($value, 0, '', $lang === 'fr' ? "\u{202F}" : ',');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\materio_sapi\Unit;
|
||||
|
||||
use Drupal\materio_sapi\SearchMessage;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
|
||||
/**
|
||||
* Tests the messages above the search results (F4), in English.
|
||||
*/
|
||||
#[CoversClass(SearchMessage::class)]
|
||||
#[Group('materio_sapi')]
|
||||
final class SearchMessageTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* Cases of a search: exact matches, all results, expected message.
|
||||
*/
|
||||
public static function resultsProvider(): array {
|
||||
return [
|
||||
'nothing' => [0, 0, 'No results for “bois”. Try other words or a broader description.'],
|
||||
'one close' => [0, 1, 'No exact result, 1 close result for “bois”'],
|
||||
'close only' => [0, 266, 'No exact result, 266 close results for “bois”'],
|
||||
'one exact alone' => [1, 1, '1 exact result for “bois”'],
|
||||
'one exact' => [1, 283, '1 exact result out of 283 relevant results for “bois”'],
|
||||
'exacts' => [7, 283, '7 exact results out of 283 relevant results for “bois”'],
|
||||
'thousands' => [35, 1475, '35 exact results out of 1,475 relevant results for “bois”'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Plurals, and "close" results without any exact match.
|
||||
*/
|
||||
#[DataProvider('resultsProvider')]
|
||||
public function testResults(int $exact, int $total, string $expected): void {
|
||||
$message = new SearchMessage($this->getStringTranslationStub());
|
||||
$this->assertSame($expected, (string) $message->results($exact, $total, 'bois', 'en'));
|
||||
}
|
||||
|
||||
/**
|
||||
* The help before any search gives the size of the catalogue.
|
||||
*/
|
||||
public function testHelp(): void {
|
||||
$message = new SearchMessage($this->getStringTranslationStub());
|
||||
$this->assertSame('Please use the search form to search from our 8,073 materials.', (string) $message->help(8073, 'en'));
|
||||
}
|
||||
|
||||
/**
|
||||
* French numbers use a narrow no-break space.
|
||||
*/
|
||||
public function testNumber(): void {
|
||||
$this->assertSame("8\u{202F}073", SearchMessage::number(8073, 'fr'));
|
||||
$this->assertSame('8,073', SearchMessage::number(8073, 'en'));
|
||||
$this->assertSame('283', SearchMessage::number(283, 'fr'));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user