TranslatorControl Class

Bing
 

The TranslatorControl class represents a control that takes a string, and a source and a target language specification, sends them to the Microsoft Translator web service, and receives the text returned by Translator. You can incorporate the control into your application, passing strings to it, and receiving the translation back.

Published date: October 21, 2013

System_CAPS_warningWarning

The Bing Translator Service cannot be called except by using the Translator Control.

public sealed class TranslatorControl : Control
<Translator:TranslatorControl .../>

The TranslatorControl class has the following members.

Name

Description

TranslatorControl()

Creates an instance of the Bing TranslatorControl.

Name

Description

Category

If you are using a customized translation system, the category value is the identifier of the customized system. Provided by the Microsoft Translator Hub.

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 Bing Translator Control under. It is used for authentication and billing when calling the Bing Translator Service.

ErrorCode

The error code that results from translating from one language to another language

LanguageFrom

Specifies the language code of the language to translate from.

LanguageTo

Specifies the language code of the language to translate to.

TextToTranslate

The text to translate from one language to another.

TextTranslated

The translation of the TextToTranslate property value, from the language specified in LanguageFrom to the language specified in LanguageTo.

TranslatorAPI

A TranslatorLibrary Class object that hosts the methods associated with the Translator Control.

Name

Description

Completed

Raised when The Bing Translator Control receives a response from the Bing Translator Service. The service response is stored in the TranslationResult object associated with this event.

Failed

Raised when an error occurs in the control, such as when the control is unable to contact the Bing Translator Service.

The Translator Control does not have any UI. You must acquire the source string and the source and target languages, and render the translation, via other controls.

The methods associated with this control reside in the TranslatorLibrary Class. To call them, use the TranslatorAPI Property.

Example

The following example implements the Translator Control in a XAML environment. For a JavaScript example, see the TranslatorLibrary Class documentation.

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:C_SharpApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Translator="using:Bing.Translator"
    x:Class="C_SharpApp.MainPage"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

        <Translator:TranslatorControl x:Name="Translator2" ClientId="your Client ID" 
             ClientSecret="your Client Secret" LanguageFrom="en" LanguageTo="es" />
        <TextBox Text="{Binding TextToTranslate, ElementName=Translator2, Mode=TwoWay}" 
             x:Name="textbox1"/>
        <TextBox Text="{Binding TextTranslated, ElementName=Translator2}" />
        <TextBox Text="{Binding ErrorCode, ElementName=Translator2}" />
        <ComboBox x:Name="cbLanguages" SelectionChanged="cbLanguages_SelectionChanged" />
        <TextBox x:Name="LangTextBox" Text="" />
        <TextBlock>
        <Run Text="TextToTranslate" />
        <LineBreak/>
        <Run/>
        </TextBlock>
        <TextBlock Text="TextTranslated&#x9;" />
        <TextBlock >
        <Run Text="ErrorCode" />
        <LineBreak />
        <Run />
        </TextBlock>

    </Grid>
</Page>

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace C_SharpApp
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            Initialize();
        }

        public async void Initialize()
        {
            // Get the list of supported languages.
            var result = await this.Translator2.TranslatorApi.GetLanguagesAsync();
            this.cbLanguages.ItemsSource = result.Languages;
        }

        private async void cbLanguages_SelectionChanged(
          object sender, SelectionChangedEventArgs e)
        {
            ComboBox b = sender as ComboBox;
            string lang = b.SelectedItem as string;

            // Get the names of the languages.
            this.LangTextBox.Text = string.Join(",", (
             await this.Translator2.TranslatorApi.GetLanguageNamesAsync(
             "en", new string[] { lang })).Languages);
        }
    }
}

Requirements

Minimum Supported Client

Windows 8.1

Required Extensions

Bing Translator Control

Namespace

Bing.Translator

Show: