这里的另一个解决方案:https://zargony.com/2008/01/24/links-in-gettext-translated-strings。
它建议使用{curly brackets} 链接。比如:
_("Please click {here} or {here}")
然后编写自己的函数linkify,用给定的参数替换大括号中的内容。
linkify(
_("Please click {here} or {here}"),
"</a>",
"<a href='www.example1.com'>",
"<a href='www.example2.com'>"
)
PHP 中的定义:
function linkify($string, $closingTag) {
$arguments = func_get_args();
return preg_replace_callback(
'/{(.*?)}/', // Ungreedy (*?)
function($matches) use ($arguments, $closingTag) {
static $i = 1;
$i++;
return $arguments[$i] . $matches[1] . $closingTag;
},
$string
);
}
PS:您也可以轻松地将 {} 替换为 [],因为我注意到在 POEdit 中,这些大括号会向翻译人员发出警告并建议不要翻译。