TranslatorLibrary Class

Bing
 

The TranslatorLibrary class hosts the public methods of The Bing Translator Control. This class applies to the TranslatorControl.TranslatorAPI Property.

Published date: October 21, 2013

public sealed class TranslatorLibrary

The TranslatorLibrary class has the following members.

Name

Description

TranslatorLibrary()

Creates an instance of the TranslatorLibrary class.

Name

Description

ClientID

Holds the ID of the Azure Data Marketplace account that the application developer registered the Translator Control under. It is used for authentication and billing when calling the Bing Translator Service.

ClientSecret

Holds the Client Secret value of the Azure Data Marketplace account that the application developer registered the Translator Control under. It is used for authentication and billing when calling the Bing Translator Service.

Name

Description

TranslateAsync(string, string, string, string)

Translates a text string from one language to another. Return value is the translated string.

GetLanguagesAsync()

Returns an array of language IDs that the Translator control can translate from or to. The Translator control can translate any of the listed languages to any other listed language.

GetLanguageNamesAsync(string, string[])

Returns an array of human-readable friendly names of languages, in the requested language.

The TranslatorLibrary class is an intermediary class to facilitate method access in JavaScript.

Example

The following example implements the Translator Control in JavaScript on an HTML page. For a XAML implementation in C#, see the TranslatorControl Class documentation.

<!DOCTYPE html>
<html>
<head>    
    <title>TranslatorApp</title>   
    <script>
        var Trans = Bing.Translator.TranslatorLibrary(
            "YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
        var LanguageCodes = [];
        var LanguageNames = [];

        function PopulateLanguages() {
            Trans.getLanguagesAsync().then(function (result) {
                LanguageCodes = result.languages;
                Trans.getLanguageNamesAsync(
                    "en", LanguageCodes).then(function (result) {
                    LanguageNames = result.languages;
                    FillList('LanguageFromList');
                    FillList('LanguageToList');
                })
            });
        }

        function FillList(list) {
            for (var i = 0; i < LanguageCodes.length; i++) {
                var opt = document.createElement("option");
                opt.innerHTML = LanguageNames[i];
                document.getElementById(list).appendChild(opt);
            }
        }

        function GoButton_Click() {
            var TranslateFrom = LanguageCodes[document.getElementById(
                'LanguageFromList').selectedIndex];
            var TranslateTo = LanguageCodes[document.getElementById(
                'LanguageToList').selectedIndex];
            var txt = document.getElementById('InputText').innerText

            Trans.translateAsync(TranslateFrom, TranslateTo, "", txt
            ).then(function (result) {
                document.getElementById('OutputText').innerText
                    = result.textTranslated;
            });
        }
    </script>

</head>
<body onload="PopulateLanguages()">
    <form>
        <div>
            Translate from <br />
            <select id="LanguageFromList"></select>
        </div>
        <div>
            Translate to <br />
            <select id="LanguageToList"></select>
        </div>
        <div>
            Text to translate <br />
            <textarea id="InputText"></textarea>
        </div>
        <div>
            Returned text <br />
            <textarea id="OutputText"></textarea>
        </div>
        <input type="button" onclick="GoButton_Click()" 
            value="Translate" />
    </form>
</body>
</html>

Requirements

Minimum Supported Client

Windows 8.1

Required Extensions

Bing Translator Control

Namespace

Bing.Translator

Show: