Microsoft Translate API Errors

Like any good dev, I have side projects. And like any good busy dev, I don't get enough time to finish them properly.

So this time of the year is always productive as i try and get stuff done before returning to work.

Today I needed to translate a whole bunch of resource strings into a bunch of other languages. So I thought the Bing Translate Api would be a good fit. So off I went to Azure Marketplace to add the service to my Azure subscription.

And right away I started running into problems - its nowhere to be found.

To cut a long story short - it seems to have been superceded by the Microsoft Translator API.

Now. Signing up and adding it to my existing subscription is simplicity itself.

And when you get to the dashboard for the service, you get something that looks like this:

alt

The proxy class is nice and compact and excellent and saves the hassle of adding a full blown service reference to your VS2013 project.

Then they give you this small code snippit to use the proxy class:

var client = new TranslatorContainer(new Uri("https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/v1/"));
client.Credentials = new NetworkCredential("accountKey", "Insert Account Key");
var marketData = client.Translate(
"hello",
"nl",
null
).Execute();

(obviously you need to swap "Insert Account Key" with your actual account key)

One thing to note here is that this is using V1 of the API - and its just for OData (tho this doesn't preclude you from calling it from javascript). This API is fairly basic. There is a more powerful V2 API floating about that requires a bit more elaborate authentication.

(The difference between the two versions caught me out for a while.)

Now, when I went to use the proxy class right out of the box, I got an entity mismatch error:

"There is a type mismatch between the client and the service. Type 'Microsoft.Translation' is not an entity type, but the type in the response payload represents an entity type. Please ensure that types defined on the client match the data model of the service, or update the service reference on the client."

Now if you open Fiddler and allow HTTP decryption, you'll see that the request actually suceeds. So the error is not in the call, or the authentication (as I initially thought), but in parsing the response.

And if we look at the error text, we see that it spells out eaxtly what the issue is - 'Microsoft.Translation' is not an entity type

Of course, being the doofus that I occasonally am, I missed that completely at first.

Once I noticed that, it hit me - I simply need to tell the DataServiceContext that Microsoft.Translation is an entity type.

And how do you do that?

Enter the [DataServiceEntity] attribute.

So. Open up the proxy class you downloaded from the Azure dashboard page and decorate the Translation, Language and DetectedLanguage classes with [DataServiceEntity] and you're good to go.

Your code should look like this:

[DataServiceEntity]
public partial class Translation {

    private String _Text;

    public String Text {
        get {
            return this._Text;
        }
        set {
            this._Text = value;
        }
    }
}
[DataServiceEntity]
public partial class Language {

    private String _Code;

    public String Code {
        get {
            return this._Code;
        }
        set {
            this._Code = value;
        }
    }
}

[DataServiceEntity]
public partial class DetectedLanguage {

    private String _Code;

    public String Code {
        get {
            return this._Code;
        }
        set {
            this._Code = value;
        }
    }
}

And if anyone knows anyone at Microsoft, hopefully the proxy class file can be updated.