谷歌翻译付费API密钥问题
我已将谷歌翻译器集成到我的网站中。一切正常。
在限制免费谷歌选项后,我使用以下带有付费 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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