首页 > 解决方案 > 我需要将博客文章分块发送到谷歌翻译并返回

问题描述

我需要分块发送博客文章,最好是用句子或单词来翻译并返回我已经有了翻译功能我只需要帮助将它分成块并取回我如何才能做到这一点?

//["this","is","the","post"]
var bod = a.body.split(" ");

            var counter = 0;


            while(counter < bod.length){

                counter++;


                var rang = bod.slice(counter, 10);


    //then translate the remaining portion with the code that is ready


                        console.log(rang);

                if(counter === bod.length || counter > bod.length){
                    console.log("end");
                    return;
                }



                        }

标签: javascriptnode.jsgoogle-translategoogle-translation-api

解决方案


你在正确的轨道上。

无需详细了解您对 google 的请求,请执行以下操作:

//Sample input
var body = 'The words I want to translate!';
//Sample result, will be an array containing translated words
var translated = translateData(body);

    //Sample function
    function translateData(body){
        var bodyArray = body.split(' ');
        var translatedData = [];
        for(var i = 0;i<bodyArray.Length;i++){
            //Do the translation here. var translated is the translation for the word
            var translated = GoogleTranslate(bodyArray[i]);
            translatedData.push(translated);
    }
        return translatedData;
}

推荐阅读