1

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

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

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

4

2 回答 2

1

对于简单的国际化,我将简单地为每种语言创建一个数组,包含正确的文件,然后访问该数组,而不是做你正在做的事情。

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

索引.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 文件。

于 2009-09-28T01:03:02.507 回答
0

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

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

当然,您可以用对您和您的翻译人员有意义的任何内容替换<a>和。</a>想一想,因为您必须转义输出以防止翻译者弄乱您的 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);
于 2009-09-28T01:01:36.737 回答