gettext でリンクを含むテキストを実行する良い方法はありますか?
質問
現在、PHPを使用してgettextで国際化を行っています。この例に何か良い方法はないかと考えていました。
By using this website, you accept the <a href="<?php print($dir); ?>/tos/">Terms of Use</a>.
en_US の場合、この文はこのような形式になります。ただし、別の言語では、「利用規約」というリンクが文の先頭にある場合があります。これを行うエレガントな方法はありますか?御時間ありがとうございます。
解決
ここで私はそれを行うだろう方法です。翻訳する句は次のようになります。
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);
他のヒント
単純な国際化の場合は、実行していることを実行するのではなく、単純に言語ごとに配列を作成し、適切なファイルをインクルードし、その配列にアクセスします。
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 ファイルに安全にアクセスできなくなります。
所属していません StackOverflow