在 gettext 中使用链接处理文本的好方法?

发布于 2024-08-05 18:47:30 字数 272 浏览 5 评论 0原文

我目前正在使用 PHP 进行 gettext 国际化。我想知道这个例子有没有什么好的方法:

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

对于en_US,这句话会遵循这样的格式。然而,在另一种语言中,“使用条款”链接可能位于句子的开头。有没有一种优雅的方法来做到这一点?感谢您抽出时间。

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>.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So要识趣 2024-08-12 18:47:30

对于简单的国际化,我将简单地为每种语言创建一个数组,包含适当的文件,然后访问该数组,而不是执行您正在执行的操作。

en.php:

$text = array(
     'footer' => 'By using this website, you accept the <a href="' . $dir . '/tos/">Terms of Use</a>.',
     'welcome' => 'Welcome to our website!'
);

index.php:

$langdir = '/path/to/languages';
$lang = ( $_GET['lang'] ) ? $langdir . '/' . $_GET['lang'] . '.php' : $langdir . '/en.php';
if( dirname($lang) != $langdir || !require_once( $lang ) )
     exit('Language does not exist');

echo '<p class="footer">' . $text['footer'] . '</p>';

dirname() 调用至关重要;否则用户可以未经审查地访问文件系统上的任何 php 文件。

For simple internationalization, I'll simply create an array per language, include the proper file, and access that array rather than do what you are doing.

en.php:

$text = array(
     'footer' => 'By using this website, you accept the <a href="' . $dir . '/tos/">Terms of Use</a>.',
     'welcome' => 'Welcome to our website!'
);

index.php:

$langdir = '/path/to/languages';
$lang = ( $_GET['lang'] ) ? $langdir . '/' . $_GET['lang'] . '.php' : $langdir . '/en.php';
if( dirname($lang) != $langdir || !require_once( $lang ) )
     exit('Language does not exist');

echo '<p class="footer">' . $text['footer'] . '</p>';

The dirname() call is critical; otherwise users get unvetted access to any php file on your filesystem.

悲念泪 2024-08-12 18:47:30

我就是这样做的。要翻译的短语是

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

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

$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);

当然您可以替换 任何对您和您的翻译人员有意义的内容。想想看,因为你必须转义你的输出以防止翻译者弄乱你的 HTML(有意或无意),我可能会选择类似的东西

$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);

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文