11

In Magento 1 I use this code:

<?php echo $this->__('some <a href="%s">text here</a>', Mage::getUrl('some/url/here')); ?>

How can I use in Magento 2?

4 Answers 4

10

Your solution might look something like this:

<a href="<?php echo $block->escapeUrl($this->getBaseUrl().'customer/address/');?>">
   <?php echo $block->escapeHtml(__('Street Address %1', $_i)); ?>
</a>

All URLs and translated content should be escaped to prevent XSS. Also, HTML tags shouldn't be translated.

Also, you can wrap this code by an own method.

1
  • can we use %s instead of %1? Oct 23, 2018 at 6:48
10

HTML code should not be used directly in a translation string, instead use a placeholder (i.e. %1) to represent it, and put it's value in a subsequent parameter to the translation method.

<?= __('some %1text here%2', '<a href="' . $block->getUrl('some/url/here') .'">', '</a>') ?>

The Magento core team uses this approach throughout their code base.

For example in...

vendor/magento/module-catalog/view/frontend/templates/product/list/toolbar/amount.phtml

1
  • I don't agree that HTML code should not be used directly in a translation string. I also found different approach of using in Magento core files. Please check my answer above.
    – bdn
    Nov 2, 2021 at 10:51
0

Magento2 give possibilities to translate links and use it. It could be safely done in this way:

__('Only registered users can write reviews. Please <a href="%1">Sign in</a> or <a href="%2">create an account</a>', $block->getLoginLink(), $block->getRegisterUrl())

where %1 is $block->getLoginLink() and %2 is $block->getRegisterUrl()

BUT for correct showing on the frontend, need to use escapeHtml this translation with allowed tags for escaping $block->escapeHtml(...)

So, to show url like typical <a href=""> tag, we need to do the following:

$block->escapeHtml(__('Only registered users can write reviews. Please <a href="%1">Sign in</a> or <a href="%2">create an account</a>', $block->getLoginLink(), $block->getRegisterUrl()), ['a'])

where add ['a'] tag like allowed tag for escaping.

In translation file (i18n/en_US.csv), it should looks like:

"Only registered users can write reviews. Please <a href=""%1"">Sign in</a> or <a href=""%2"">create an account</a>","Only registered users can write reviews. Please <a href=""%1"">Sign in</a> or <a href=""%2"">create an account</a>"

P.S. I took this example from core file: vendor/magento/module-review/view/frontend/templates/form.phtml , so I think, this approach is also valid

-1

Use like this

<?= $block->escapeHtml(__('Street Address %1', $_i)) ?>

or

<?= $block->escapeHtml(__('Customer address <a href="%s">link</a>!', $block->escapeUrl($this->getBaseUrl().'customer/address/'))) ?>

The string in the CSV file should have double quotes:

"Customer address <a href=""%s"">link</a>!"
7
  • Any html tags should not be in the translation function.
    – joni jones
    Jun 10, 2016 at 9:12
  • @jonijones Incorrect. Sometimes you need to translate strings where a link could appear in the middle. Nov 2, 2016 at 18:58
  • 2
    This isn't correct, you use sprintf() after Magento translated the string: echo sprintf( __('My string with <a href="%s">link</a>') , $myLink ) and the string in the CSV file should have double quotes: "My string with <a href=""%s"">link</a>" Nov 2, 2016 at 19:29
  • @DesignbyAdrian, a string should be translated without reference to any HTML tags. You don't translate tags, you translate text output. Also, translation can break tags.
    – joni jones
    Nov 3, 2016 at 7:59
  • @jonijones But a link can appear in different places in a sentence depending on language. I know it's wrong, but if the client wants only part of a sentence to be highligted, what's are the options? Nov 3, 2016 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.