Reputation: 421
I am trying to convert English text to Hindi text. For that I am using Microsoft Text translator API. But some of my text is not translating.
After googling I have found below code.
public string GetHindiText(string textToTranslate)
{
Carrier.ErrorCode = 0;
string TranslatedText = string.Empty;
try
{
Translator.LanguageServiceClient objTranslate = new Translator.LanguageServiceClient();
TranslatedText = objTranslate.Translate("***************", textToTranslate, "en", "hi");
}
catch (WebException ex)
{
Carrier.ErrorCode = 1;
return ex.Message;
}
return TranslatedText;
}
And I have added below service reference
http://api.microsofttranslator.com/V1/SOAP.svc
Most of the text is not translating. I need your suggestions to move forward. Am I doing in correct way or anything I need to change. Any help is greatly helpful to me.
Thanks in advance.
Upvotes: 3
Views: 925
Reputation: 647
Refer to the NuGet package NequeoNetTranslator, this contains text and speech translation APIs.
Sample of text translation this uses the new Cognitive
version, first get your access token, through your translation [KEY]:
Nequeo.Net.Translator.Microsoft.Cognitive.Api apiat = new Nequeo.Net.Translator.Microsoft.Cognitive.Api(new Uri("https://api.cognitive.microsoft.com/sts/v1.0/"));
apiat.Credentials = new System.Net.NetworkCredential("[KEY]", "[KEY]");
string token = apiat.GetAccessToken();
Now call the translate method , this will translate en
tode
:
Nequeo.Net.Translator.Microsoft.Cognitive.Api api = new Nequeo.Net.Translator.Microsoft.Cognitive.Api(new Uri("https://api.microsofttranslator.com/v2/http.svc/"));
api.Credentials = new System.Net.NetworkCredential("[KEY]", "[KEY]");
byte[] data = api.Translate("hello", "de", "en", null, token);
Translation[] tran = api.Translate(data);
string tranText = System.Text.Encoding.Default.GetString(data);
If you are using speech to text then sample code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Nequeo.Net.Translator.Microsoft.Cognitive.SpeechApi apiat = null;
private void button1_Click(object sender, EventArgs e)
{
apiat = new Nequeo.Net.Translator.Microsoft.Cognitive.SpeechApi(new Uri("wss://dev.microsofttranslator.com/speech/"));
apiat.Credentials = new System.Net.NetworkCredential("[KEY]", "[KEY]");
string token = apiat.GetAccessToken(new Uri("https://api.cognitive.microsoft.com/sts/v1.0/"));
apiat.OnRecording += Apiat_OnRecording;
apiat.OnStopRecording += Apiat_OnStopRecording;
apiat.OnTranslationReceived += Apiat_OnTranslationReceived;
Nequeo.IO.Audio.Device device_in = Nequeo.IO.Audio.Devices.GetDeviceIn(0);
apiat.AudioDevice = device_in;
apiat.WriteStream = new System.IO.MemoryStream();
apiat.Translate("hr-HR", "en-US", token);
}
private void Apiat_OnTranslationReceived(object sender, EventArgs e)
{
System.IO.MemoryStream jj = (System.IO.MemoryStream)apiat.WriteStream;
string gg = Encoding.Default.GetString(jj.ToArray());
Nequeo.Net.Translator.Microsoft.Cognitive.SpeechTranslation dffddf = apiat.GetSpeechTranslation();
}
private void Apiat_OnStopRecording(object sender, EventArgs e)
{
bool kk = true;
}
private void Apiat_OnRecording(object sender, EventArgs e)
{
bool kk = true;
}
private void button2_Click(object sender, EventArgs e)
{
apiat.StopTranslate();
}
}
Upvotes: 3