Bing translation services support many languages, but we need to submit the proper language codes in our calls. In this video, learn how to get the supported languages in the Bing Translator API.
- [Instructor] At this point, you should have the translate API registered in Microsoft. They'll give you, under the overview section, they'll give you an endpoint, and you can click on the Show Access Keys, and you should be able to see your keys here. Sometimes it errors out, let me click on it one more time. So you can click on this keys button, and that should show you your keys. Again, your keys will be different so try not to use mine. In fact, they won't work because I will have deleted them by the time you are watching this course.
Anyway, there's one thing I'd like to show you though. Let's go into the translate API one more time, and click on the Overview link. Now, you see this endpoint? Again, it's that STS end point. So this endpoint again, is you sign into your key, you get an access token, and then you would continue using the actual functionality which is translation, in this case. That is certainly the preferred approach, I did show that approach in one of the previous examples.
However, I'm going to use the simpler approach here, so if you go to the Quick start area, and you click on this API reference, let's go ahead and open this, and you go down here, just scroll down and it says Authorization, so there are two different ways of doing so. One is that you can do so by getting the access token, and I did demonstrate this in a previous chapter under the speech sections, so this applies to most cog services that you can get the access token first and then call the service, or you can pass the subscription key directly.
I prefer the approach of getting the access token first. It's certainly a lot more secure because the access token changes every hour or so, whereas a subscription key is a lot more longer in duration. It also gives me other ways, or other architectural paradigms, but here, to keep our codes simple, that's the only reason, I will choose the second approach which is using the subscription key directly and passing in my key as the Ocp-Apim-Subscription-Key header, so that's the approach I'll be using here.
Okay, let's go ahead and close the browser and get into code. And you see here that in my code I have marked an endpoint which is not the STS endpoint. This endpoint is directly the translator service endpoint, and the two API keys that I got from the portal. Okay, now let's go to index.ts and start writing some code. So let's say that I want a simple function translateText, and I'm going to say that this is some sample text, or you can write in whatever you want.
And I want to pass in the language that I want to translate from and the language that I want to translate to. Well where do I get these codes from? Well you know what, the API itself can be used to give me the various languages it supports. So getSupportedLanguages. Let's say that I had a function like that, so let's do that function first. So here I'm going to say function getSupportedLanguages, and let's craft up our requestOptions and headers.
And you remember that header that we had to pass in, Ocp-Apim-Subscription-Key Let's go ahead and pass it our key. In this header, I'm going to take this header, and I'm going to make a simple get request. Request dot get to config dot endPoint plus slash GetLanguagesForTranslate.
There's a whole API reference help they have where they give you these possibilities like what are the things that the API supports. So this is one of the things that it supports. Give it the requestOptions and as usual err, response, body. And I'm going to simply console dot dir out the response that I got from the server. Let's go ahead and run this.
Set a breakpoint there, hit F5. And you see here that it wrote out some response and it writes out as strings and these are the languages that apparently we support, that's quite a few. So if you go through this, you'll see that it does support en and it does support de, so English to German should work. Okay so now that we have the languages the next thing to do is to simply do the actual translation.
- Exploring the possibilities of the Vision API
- Submitting an image to the Vision API for processing
- Asking the Vision API to recognize faces
- Working with the Speech API
- Writing speech-to-text code
- Working with the Language API
- Getting languages for translation
- Language Understanding (LUIS) concepts