Support TranslatableMessage as a label value #31
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.
This is a reopening of PR 29.
I added two unit tests and a line in the readme to tell about the feature. I'm not used to testing in PHP yet so let me know if there is something I can improve.
Copying the original message below.
Currently, labels can only be a static string, a translated string without parameters, or a value retrieved from a property path. It would be nice to have a system for translated strings with parameters. My use case is that for some breadcrumb I would like to prepend something to a property, and/or surround a property with quotes (e.g. instead of rendering the dynamic value prop as prop, I would like Property : “prop”).
This PR implements this use case by allowing the property path given as label to resolve to a TranslatableMessage instead of a plain string. It respects the translationDomain setting except that false is not considered differently than null so that the default domain is used (indeed it would make no sense to disable translation on a TranslatableMessage).
This allows setting up a breadcrumb like so:
// The breadcrumb uses the breadcrumbLabel property of a Tier object.
const array TIER_BREADCRUMB = [
'label' => '$tier.breadcrumbLabel',
'route' => 'app_tier_detail',
'params' => ['id' => '$tier.id'],
'translationDomain' => null,
];
// The breadcrumbLabel property returns a TranslatableMessage instead of a plain string.
class Tier {
public function getBreadcrumbLabel(): TranslatableMessage {
return new TranslatableMessage('trans_str_with_params', ['%param%' => $this->anotherProperty]);
}
}
// The Route is not relevant here.
#[Route('/tiers/{id}', name: 'app_tier_detail')]
#[Breadcrumb(TIER_BREADCRUMB)]
public function myRoute(int $id) { /* … */ }
Let me know what you think!