Google跨多个网页翻译

时间:2019-04-26 11:24:20

标签: google-translate

我已经创建了两个示例网页,如果我从第1页的下拉菜单中选择语言,我希望该页面具有即使刷新后仍被选择的相同语言。另外,如果我访问其他页面,我也希望该页面也被翻译。

我已经在网站上合并了Google翻译API。但是由于该API已关闭,因此我无法获得相应的文档。我试图通过将其存储在cookie中来存储值,但是我错过了某些东西。

<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8"/>
<script type="text/javascript" src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

<script src="https://code.jquery.com/jquery-3.4.0.min.js" integrity="sha256-BJeo0qm959uMBGb65z40ejJYGSgR7REI4+CW1fNKwOg=" crossorigin="anonymous"></script>


<script type="text/javascript">
function googleTranslateElementInit() {
  new google.translate.TranslateElement(
    {
      pageLanguage: 'en-US',
      includedLanguages: 'zh-TW,zh-CN,hi,ta,ur',
      layout: google.translate.TranslateElement.InlineLayout.SIMPLE, 
      autoDisplay: false
    },
    'google_translate_element'
  );
}

<!-- function googleTranslateElementInit(){ -->
<!-- new google.translate.TranslateElement -->
<!-- ({ -->
<!-- pageLanguage:'en', -->
<!-- layout:google.translate.TranslateElement.InlineLayout.SIMPLE,gaTrack:true,gaId:''},'google_translate_element'); -->
<!-- $('#google_translate_element span > a > span:nth-child(5)').attr('role','menuitem');} -->
function readCookie(name) {
    var c = document.cookie.split('; '),
    cookies = {}, i, C;

    for (i = c.length - 1; i >= 0; i--) {
        C = c[i].split('=');
        cookies[C[0]] = C[1];
     }

     return cookies[name];
}
console.log(readCookie('googtrans'));

</script>
</head>
<body>
<a href="translate1.html"> Go to Page1</a>
<h1>Page2</h1>

<p>This is second page on the website</p>

<p>lets see whether translation works here</p>

<div id="google_translate_element"></div>

<!-- <script type="text/javascript"> -->
<!-- function googleTranslateElementInit() { -->
  <!-- new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element'); -->
<!-- } -->
<!-- </script> -->

<!-- <script type="text/javascript" src="C:\Users\Dellpc\Desktop\IE\Translate\element.js?cb=googleTranslateElementInit"></script> -->

<p>this is page2</p>

      <script type="application/javascript" src="https://server/cookies.js"></script>

</body>
</html>

当前,如果您运行此文件。刷新后,文件将更改为英语的原始文件。另外,如果我转到另一页,则不会翻译成第一页上选择的语言。如何使Google翻译价值在整个会话中保持不变?

0 个答案:

没有答案