[Autocomplete] Translate the optgroup labels returned by the AJAX endpoint - #3763
Open
kira0269 wants to merge 1 commit into
Open
[Autocomplete] Translate the optgroup labels returned by the AJAX endpoint#3763kira0269 wants to merge 1 commit into
kira0269 wants to merge 1 commit into
Conversation
…point The labels produced by the "group_by" option are translated by the form theme when the <select> is rendered, but the ones sent as JSON by the autocomplete endpoint were emitted verbatim. For an AJAX autocompleter, every group the user sees while typing comes from that JSON, so the labels stayed untranslated, and they no longer matched the server rendered <optgroup> the Stimulus controller looks up by label. The executor now translates them, using the domain returned by the new optional getTranslationDomain() method of the autocompleter, which the entity autocomplete fields read from their "choice_translation_domain" option. The optgroup "value" keeps the untranslated label so that it still ties the results to their group whatever the locale is. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
group_byoption of an autocomplete field produces the<optgroup>labels. When the<select>is rendered by the form theme, those labels go through|trans:But the labels sent as JSON by the autocomplete endpoint are emitted verbatim by
AutocompleteResultsExecutor:For an AJAX autocompleter this is the path that matters: the server-rendered
<select>only holds the preselected option(s), so every group the user sees while typing comes from that JSON. Using a translation key ingroup_bytherefore shows the raw key. It also makes the labels disagree with the server-rendered ones, which the Stimulus controller looks up by label:What this PR does
AutocompleteResultsExecutortakes an optionalTranslatorInterfaceand translates the optgroup labels. Values returned bygroup_bythat implementTranslatableInterfaceare no longer cast to string either (casting aTranslatableMessageis a fatal error).getTranslationDomain(): string|false|nullmethod on the autocompleter —nulluses the default domain,falsedisables the translation. It is declared as a@methodonEntityAutocompleterInterfaceand commented out in the interface body, likegetGroupBy(), and detected withmethod_exists(), so existing autocompleters keep working untouched.WrappedEntityTypeAutocompleterimplements it by reading the field'schoice_translation_domainoption, which is exactly what the form theme uses. So the AJAX results and the rendered<select>now agree.valuekeeps the untranslated label, so it still ties a result to its group whatever the locale is. Only thelabelis translated.BC
The translator is an optional constructor argument wired with
NULL_ON_INVALID_REFERENCE, and without it the previous behavior is kept. Autocompleters that do not implementgetTranslationDomain()translate in the default domain, which matches what the form theme already does for the same labels.