用gettext中的链接编写文本的好方法? [英] Good method to do text with links in gettext?

查看:73
本文介绍了用gettext中的链接编写文本的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用PHP使用gettext进行国际化.我想知道此示例是否有任何好的方法:

I'm currently doing internationalization with gettext using PHP. I was wondering if there were any good methods for this example:

By using this website, you accept the <a href="<?php print($dir); ?>/tos/">Terms of Use</a>.

对于en_US,此句子将采用这种格式.但是,用另一种语言,使用条款"链接可能位于句子的开头.有没有一种优雅的方法可以做到这一点?谢谢您的宝贵时间.

For en_US, this sentence would follow such a format. However, in another language, the link "Terms of Use" could be at the beginning of the sentence. Is there an elegant way to do this? Thanks for your time.

推荐答案

这就是我要做的事情.要翻译的短语是

Here's how I'd do it. The phrase to be translated would be

By using this website, you accept the <a>Terms of Use</a>.

然后我将短语本地化后替换链接,例如

Then I'd replace the link after the phrase is localised, e.g.

$str = _('By using this website, you accept the <a>Terms of Use</a>.');
$str = preg_replace('#<a>(.*?)</a>#', '<a href="' . $dir . '/tos/">$1</a>', $str);

当然,您可以使用对您和您的翻译人员有意义的任何内容替换<a></a>.仔细考虑一下,因为您必须转义输出以防止翻译人员(有意或无意地)弄乱HTML,所以我可能会选择

Of course you can replace <a> and </a> with whatever makes sense to you and your translators. Come to think of it, since you have to escape your output to prevent translators from messing up with your HTML (intentionally or not) I'd probably go with something like

$str = htmlspecialchars(_('By using this website, you accept the [tos_link]Terms of Use[/tos_link].'));
$str = preg_replace('#\\[tos_link\\](.*?)\\[/tos_link\\]#', '<a href="' . $dir . '/tos/">$1</a>', $str);

这篇关于用gettext中的链接编写文本的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆