Files
materio-d9/web/modules/custom/materio_sapi/src/SearchMessage.php
T
bachirandClaude Opus 5.5 1e882d5bda 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
2026-11-23 16:44:28 +01:00

72 lines
2.5 KiB
PHP

<?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}" : ',');
}
}