Yii: How to work with translate Yii::t() and hyperlinks
Solution 1
The link may have different placement (beginning, middle or end) and label in the translated string depending on a target language. Therefore, you should use placeholder only for url:
echo Yii::t(
'forms',
'Would you like to <a href="{url}">create a new item</a>?',
array('{url}' => '/new_item')
);
Solution 2
I suggest to you this solution:
echo Yii::t(
'forms',
'Would you like to {link:create}create a new item{/link}?',
array(
'{link:create}'=>'<a href="/new_item">',
'{/link}'=>'</a>',
)
);
The benefit is if you want put id
, class
, onclick
and more anything in a
tag you can do it. and so the translate string in clear.
Note that create
in {link:create}
is just a ideal string that pointer to hyperlink string.
Another advanced sample:
echo Yii::t(
'forms',
'Would you like to {link:create}create a new item{/link}? And you can {link:delete}delete the item{/link}.',
array(
'{link:create}'=>'<a href="/new_item" class="button">',
'{link:delete}'=>'<a href="#" id="item-21" onclick="delete(21);">',
'{/link}'=>'</a>',
)
);
Solution 3
Use following if you have a dynamic uri:
echo Yii::t(
'forms',
'Would you like to <a href=":url">create a new item?</a>',
array(':url'=>'/new_item')
);
Or:
echo Yii::t(
'forms',
'Would you like to <a href="/new_item">create a new item?</a>',
);
Or if you want to pass other dynamic attributes other than the url, use the following:
echo Yii::t(
'forms',
'Would you like to <a :linkAttr>create a new item?</a>',
array('linkAttr'=>'href="/new_item" id="link-id" class="link-class"')
);
Solution 4
I think this is a better solution:
echo Yii::t(
'forms',
'Would you like to {action}?'
[
'action' => Html::a(
Yii::t('forms', 'create a new item'),
['controller/action']
)
]
);
Benefits of this solution
- You can use helpers to generate your link
- You can modify your html code without modifing the translations
- Whoever will be doing translations doesn't need to know anything about html and they can't mess the html code.
ews2001
I am a Web Developer, working for one of the largest financial institutions in the world, by day. By night, I code the tools and applications that I wish already existed. PHP, MySQL, Yii, JQuery, Java, HTML and CSS are my favorite tools of the trade. #SOreadytohelp
Updated on June 11, 2022Comments
-
ews2001 almost 2 years
I have many lines similar to this in my code:
echo Yii::t('forms','Would you like to create a new item?');
where I want to hyperlink just around "create a new item", as an example.
Here are some alternatives that I've thought about:
-
Split the URL into 2 translated strings, surrounded by a hyperlink:
echo Yii::t('forms','Would you like to').' <a href="/new_item">'.Yii::t('forms','create a new item').'</a>?';
-
Use placeholders, as described in the Yii documentation ( http://www.yiiframework.com/doc/guide/1.1/en/topics.i18n Although hyperlinks aren't given as an explicit example):
echo Yii::t('forms','Would you like to {url}create a new item',array('{url}'=>"<a href='/new_item'>")).'</a>?';
There's probably an easier way to do this, but I've been unable to discover the preferred method...what's the best way to build translated strings that include URLs?
-