我有一个客户的网站,我们通过旗帜图标提供谷歌翻译的链接。我想从翻译结果中提供原始语言(英语)的链接。
您可以在tesselaar.com
的任意页面的右上角看到我到目前为止所做的工作如果单击其中一个标志,页面将被翻译,标志图标将替换为英文版本。我希望这个标志能够链接回来,但正如你所看到的那样,因为谷歌翻译正在重写我给它的URL。有谁知道怎么解决这个问题?
这是我的代码:
<?php
echo "<ul class=\"breadcrumb pull-right\">\n";
if (stripos($_SERVER['HTTP_REFERER'], "translate.google") !== false) { // page has been translated
if (empty($_SESSION['return_url'])) $_SESSION['return_url'] = "tesselaar.com";
echo "<li>View page in English:</li>\n";
echo "<li><a target=\"_top\" href=\"http://" . $_SESSION['return_url'] . "\"><img src=\"/assets/img/flags/gb.gif\" width=\"16\" height=\"11\" title=\"English\" alt=\"English\"></a></li>\n";
} else { // regular site (not translated)
$_SESSION['return_url'] = $_SERVER['REQUEST_URI'];
echo "<li>Translate this page:</li>\n";
echo "<li><a href=\"http://translate.google.com/translate?sl=en&tl=fr&u=tesselaar.com". $_SERVER['REQUEST_URI'] . "\"><img src=\"/assets/img/flags/fr.gif\" width=\"16\" height=\"11\" alt=\"French\"></a></li>\n";
echo "<li><a href=\"http://translate.google.com/translate?sl=en&tl=de&u=tesselaar.com". $_SERVER['REQUEST_URI'] . "\"><img src=\"/assets/img/flags/de.gif\" width=\"16\" height=\"11\" alt=\"German\"></a></li>\n";
echo "<li><a href=\"http://translate.google.com/translate?sl=en&tl=nl&u=tesselaar.com". $_SERVER['REQUEST_URI'] . "\"><img src=\"/assets/img/flags/nl.gif\" width=\"16\" height=\"11\" alt=\"Dutch\"></a></li>\n";
echo "<li><a href=\"http://translate.google.com/translate?sl=en&tl=es&u=tesselaar.com". $_SERVER['REQUEST_URI'] . "\"><img src=\"/assets/img/flags/es.gif\" width=\"16\" height=\"11\" alt=\"Spanish\"></a></li>\n";
echo "<li><a href=\"http://translate.google.com/translate?sl=en&tl=el&u=tesselaar.com". $_SERVER['REQUEST_URI'] . "\"><img src=\"/assets/img/flags/gr.gif\" width=\"16\" height=\"11\" alt=\"Greek\"></a></li>\n";
}
echo "</ul>\n\n";
?>
编辑:自从发布以来我尝试过的事情没有成功:
答案 0 :(得分:2)
所以我能用jQuery解决你的问题。这就是我所做的:
<!-- inside your <head></head -->
<script src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
<!-- inside your <body></body> -->
<?php $requestUri = str_replace('untranslate', '', $_SERVER['REQUEST_URI']); ?>
<ul class="breadcrumb pull-right">
<?php if (stripos($_SERVER['HTTP_REFERER'], "translate.google") !== false && !isset($_GET['untranslate'])) : ?>
<li class="notranslate">View page in English:</li>
<li>
<a class="untranslate" href="#">
<img src="/assets/img/flags/gb.gif" width="16" height="11" alt="English" />
</a>
</li>
<?php else: ?>
<?php $uri = "http://translate.google.com/translate?sl=en&u=" . $_SERVER['HTTP_HOST']; ?>
<li>Translate this page:</li>
<li><a href="<?php echo $uri . $requestUri ?>&tl=fr"><img src="/assets/img/flags/fr.gif" width="16" height="11" alt="French" /></a></li>
<li><a href="<?php echo $uri . $requestUri ?>&tl=de"><img src="/assets/img/flags/ed.gif" width="16" height="11" alt="German" /></a></li>
<li><a href="<?php echo $uri . $requestUri ?>&tl=nl"><img src="/assets/img/flags/nl.gif" width="16" height="11" alt="Dutch" /></a></li>
<li><a href="<?php echo $uri . $requestUri ?>&tl=es"><img src="/assets/img/flags/es.gif" width="16" height="11" alt="Spanish" /></a></li>
<li><a href="<?php echo $uri . $requestUri ?>&tl=el"><img src="/assets/img/flags/gr.gif" width="16" height="11" alt="Greek" /></a></li>
<?php endif; ?>
</ul>
<!-- right before your </body> or remove <script></script> and place in different js file -->
<script>
$('.untranslate').click(function() {
window.top.location = 'http://<?php echo $_SERVER['HTTP_HOST'] . $requestUri ?>?untranslate';
});
</script>
所以基本上没有带有类.untranslate
的url的链接然后jQuery等待点击它。点击后,它会将页面重定向到您网站的原始页面未翻译版本。此外,它还会传递一个名为untranslate
的get参数,因为重定向会传递translate.google HTTP_REFERER。