azure-cognitive-servicesbing-news-search-api

Bing News Search API language translation of results?


I am querying Bing News Search for foreign markets like China, Malaysia, Russia etc. The results are not always in English. Is there a way or a parameter I can set which would translate all results to English?

Or would I have to do this separately from the Bing News Search API by calling a different language translation API? The setLang parameter doesn't seem to work.


Solution

  • From the official documentation, setLang should not be used to set the language of search results. I checked the official documents. In 2012, Microsoft recommended using JSON Code Sample (Web SourceType). I tested it and it didn't take effect. You can view related posts.

    Bing search API - How to localize results?

    Because the existing materials are too old, they are all documents many years ago. So I recommend using Microsoft's Translator.

    Below I provide sample code, replacing the values of OcpApimSubscriptionKey1 and OcpApimSubscriptionKey1 and OcpApimSubscriptionRegion, which can be run directly. The effect diagram is as follows.

    Note:

    1. The sample code provided is only a reference code, and it needs to take into account the processing of special characters when used in a production environment.

    2. Sample code The non-optimal sample code is just my sample.

    enter image description here

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Translate</title>
        <script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
        <script>
            var OcpApimSubscriptionKey1="267****6fac1978";
            var OcpApimSubscriptionKey2="00d****bff196393";
            var OcpApimSubscriptionRegion="koreacentral";
            var tmp_jsonstr="";
            $(document).ready(function(){
    
            })
            function bingnewssearch(){
                $.ajax({
                    url: 'https://panshubeicognitiveservices.cognitiveservices.azure.com/bing/v7.0/news/search?q='+$("#keywords").val()+"&mkt=zh-CN&setLang=EN&cc=EN&count=5",
                    method: 'GET',
                    contentType: "application/x-www-form-urlencoded; charset=urf-8",
                    dataType: 'json',
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader('Ocp-Apim-Subscription-Key', OcpApimSubscriptionKey1);
                    },
                    success:function(result){
                        var myJSON = JSON.stringify(result);
                        tmp_jsonstr=myJSON;
                        $("#orginresult").html(myJSON)
                    },
                    error:function(){
                        alert("Oop, No Data Response");
                    }
                })
            }
            function translatejson(){
                var str="[{\"Text\":\""+tmp_jsonstr.replace(/\"/g,"'")+"'\"}]";
                
                var jsonData=eval( str);//JSON.parse(str);
                $.ajax({
                    url: "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=zh-Hans&to=en",
                    method: 'POST',
                    contentType: "application/json; charset=UTF-8",
                    dataType: 'json',
                    data:str,
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader('Ocp-Apim-Subscription-Key', OcpApimSubscriptionKey2);
                        xhr.setRequestHeader('Ocp-Apim-Subscription-Region', OcpApimSubscriptionRegion);
                    },
                    success:function(result){
                        var myJSON1 = JSON.stringify(result);
                        var str=result[0].translations[0].text;
                        str="{"+str.replace(/'/g, '"')+"}"
                        $("#translatedresult").html(str)
                    },
                    error:function(){
                        alert("Oop, No Data Response");
                    }
                })
            }
        </script>
    </head>
    <body>
        <div>
            <div><p>Bing News Search</p></div>
            <div>
                <input type="text" id="keywords" placeholder="key words"/>
            </div>
            <div>
                <button onclick="bingnewssearch()">Search</button>
            </div>
            <div>Orgin Result</div>
            <div>
                <span id="orginresult"></span>
            </div>
        </div>
        <hr/>
        <div>
            <div>
                <button onclick="translatejson()">translate orgindata</button>
            </div>
            
            <div>
                <span id="translatedresult"></span>
            </div>
        </div>
    </body>
    </html>