简体   繁体   English

Google Translate API-HTML文档翻译

[英]Google Translate API - html document translation

I'm trying out Google Translate API to translate my blog. 我正在尝试使用Google Translate API来翻译我的博客。 Access to the API works, I can send text and get the appropriate translation but whenever I send larger documents with HTML in it, I don't get any answer from the API anymore. 使用该API可以正常工作,我可以发送文本并获得适当的翻译,但是每当我发送包含HTML的较大文档时,API都不会再有任何答案。

I'm using PHP to send the query with the code below and I'm extracting posts from my wordpress site (= it only extracts the content between the body tags). 我正在使用PHP发送带有以下代码的查询,并且正在从我的wordpress网站提取帖子(=它仅提取body标签之间的内容)。

PHP CODE: PHP代码:

function translation ($text,$origin,$destination)
    {
        $apiKey = 'MY_API_KEY';
        $url = 'https://www.googleapis.com/language/translate/v2?key='.$apiKey.'&q='.rawurlencode($text).&source='.$origin.'&target='.$destination;
        $handle = curl_init($url);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($handle);                 
        $responseDecoded = json_decode($response, true);
        curl_close($handle);
        print_r($response);
        print_r($responseDecoded);
    }

If I feed the variables with: 如果我将变量输入:

$text: "hello world" 
$origin: "en"
$destination: "fr"

I works without any problem. 我的工作没有任何问题。

If I feed $text with html, I don't get any answer from the API, not even an error message. 如果我将$ text与html一起提供,则API不会给出任何答案,甚至不会显示错误消息。 I checked the documentation and saw this: https://cloud.google.com/translate/markup 我查看了文档,并看到了以下内容: https : //cloud.google.com/translate/markup

I tried to add the body/header part but it doesn't work but I hope I'm not obliged to add this into my code because this would require a major change on all the pages to make it work. 我试图添加正文/标题部分,但是它不起作用,但是我希望我没有义务将其添加到我的代码中,因为这将需要对所有页面进行重大更改才能使其起作用。

Do you have any idea of how I could make this work? 您对我如何进行这项工作有任何想法吗?

UPDATE: If I use substr in PHP to reduce the size of $text, I get it translated up to around 5200 chars, above this, I do not get anything any more. 更新:如果我在PHP中使用substr来减小$ text的大小,我可以将其翻译成大约5200个字符,在此之上,我什么也没得到。

Thanks! 谢谢!

Laurent 洛朗

The documentation is recommending to send less than 5000 characters per request . 该文档建议每个请求发送少于5000个字符 In your case you would need to develop a function that splits the text into chunks of size less than 5000 characters (HTML tags included) and send them in separate requests. 在您的情况下,您需要开发一个函数,将文本分成小于5000个字符(包括HTML标记)的大小块,并在单独的请求中发送它们。 However, please be aware that there are also limits with respect to characters sent per 100 seconds and requests sent per 100 seconds . 但是,请注意, 每100秒 发送的 字符数每100秒发送的请求数也有限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM