谷歌翻译付费API密钥问题

发布于 2024-12-23 03:54:45 字数 986 浏览 1 评论 0原文

我已将谷歌翻译器集成到我的网站中。一切正常。

在限制免费谷歌选项后,我使用以下带有付费 API 密钥的脚本。它对于特定内容(例如,div、span 内部内容)运行良好,

我如何将此脚本应用于整个网页。

是否可以获取整个网页内容?或者还有其他选项可以集成谷歌翻译吗?

<div id="sourceText">Some text to translate</div>
<div id="translation"></div>
<script>
  function translateText(response) {
    document.getElementById("sourceText").innerHTML = "<br>" + response.data.translations[0].translatedText;
  }
</script>
<script>
  var newScript = document.createElement('script');
  newScript.type = 'text/javascript';
  var sourceText = escape(document.getElementById("sourceText").innerHTML);
  var source = 'https://www.googleapis.com/language/translate/v2?key=MYAPIKEY=en&target='+tl+'&callback=translateText&q=' + sourceText;
  newScript.src = source;
  // When we add this script to the head, the request is sent off.
  document.getElementsByTagName('head')[0].appendChild(newScript);
  </script>

I have integrated the google translator in my website. It was working fine.

After the restriction of free google option, i am using the following script with paid API key. It is working fine for the particular content (For example, div, span inner content)

How could i apply this script to whole webpage.

Is it possible to get the whole web page content ? Or any other option there to integrate google translator ?

<div id="sourceText">Some text to translate</div>
<div id="translation"></div>
<script>
  function translateText(response) {
    document.getElementById("sourceText").innerHTML = "<br>" + response.data.translations[0].translatedText;
  }
</script>
<script>
  var newScript = document.createElement('script');
  newScript.type = 'text/javascript';
  var sourceText = escape(document.getElementById("sourceText").innerHTML);
  var source = 'https://www.googleapis.com/language/translate/v2?key=MYAPIKEY=en&target='+tl+'&callback=translateText&q=' + sourceText;
  newScript.src = source;
  // When we add this script to the head, the request is sent off.
  document.getElementsByTagName('head')[0].appendChild(newScript);
  </script>

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

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

发布评论

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

评论(1

遗忘曾经 2024-12-30 03:54:45

Google 翻译 API 主要用于系统内的单个字段或文本区域。它并不是真正适合翻译整页。首先,您要翻译的字符数量有限制。对于您的页面来说,这很可能还不够,因为 HTML 标签也会被处理。您需要在您的网站上安装谷歌翻译栏,以便用户可以通过下拉菜单选择他们的语言。

http://translate.google.com/translate_tools

The Google translator API is used mostly for single fields or textarea's within a system. It's not really ment for translating whole pages. First of all there is a restriction for the amount of characters you're going to translate. It's most likely not even enough for your pages as HTML tags are processed as well. What you need is to have the Google Translator bar installed on your site, so users can select their language via a drop-down menu.

http://translate.google.com/translate_tools

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