- Issue created by @berdir
I was a bit on the fence about creating this issue, because the current approach to the displayed message is not following recommended patterns, but we can actually work around it, and changing it is going to break that until we update it and also basically all existing configuration of any site using this.
The problem:
Translatable strings and and also (translatatable, which this isn't yet, see
✨
Make the config translatable
Needs review
) configuration strings should be complete sentences/paragraphs. That has multiple reasons:
1. Discovery on the interface translation UI, so that you can find the text and translate it
2. Context, so that it localize.drupal.org can provide a default translatation. Specifically, that means <a href="...">@text</a>
should be avoided, because you don't know what the linked text will say, so it's hard to make a valid sentence out of the whole thing
3. Flexibility, for example for left-to-right languages that will need to have the link to the cart on the start/left side of the text.
Right now, the current text is split in 3 parts, with a mix of configuration, with fallback to translatable text if empty, which is then put together to a single sentence:
$message_text = str_replace('[variation_title]', $purchased_entity->label(), $config->get('success_message'));
$url_text = str_replace('[none]', '', $config->get('cart_link_text'));
$success_message = t('@message <a href=":url">@text</a>', [
'@message' => $message_text ?: t('@entity added to', [
'@entity' => $purchased_entity->label(),
]),
':url' => Url::fromRoute('commerce_cart.page')->toString(),
'@text' => $config->get('cart_link_text') ? $url_text : t('your cart'),
]);
That works for English, but it's hard to customize. Additionally to translating, we also need to put some classes into the a tag so we can theme it. We use the non-popup mode, beta4 had a separate config, which worked quite well for us, that broke with the update.
Our workaround: while @message <a href=":url">@text</a>
is currently kind of pointless as a translatable string as there is nothing in there to translate, we can use that to actually put our translations, including customized classes all in that string and completely ignore @message and @text.
Recommended solution:
Allow for a single, fully translatable string like @entity added to <a href=":url">your cart</a>
and/or a corresponding full configuration sentence, with placeholders like the current variation_title also for other dynamic bits like the URL (although I'd recommend using the same placeholders).
Translatable only would be simplest, but wouldn't work so well for sites that aren't multilingual.
BC of this is very hard. You could try to check if certain translatable strings are customized, and if so, use those, otherwise use the new approach. But it might be easier to just keep this on the back of your mind for a possible 2.x version in the future. The current text *does* allow essentially the same end result, but you kind of have to read the source code to find that. And assuming that the @message/@string string is unique. One option for now would be to just document this workaround on the project page.
Active
1.0
Code