微软翻译 API 无法正常工作

Microsoft Translate API is not working properly

我正在尝试将英文文本转换为印地语文本。为此,我正在使用 Microsoft 文本翻译器 API。但是我的一些文字没有翻译。

谷歌搜索后我找到了下面的代码。

 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;
    }

并且我在下面添加了服务参考

http://api.microsofttranslator.com/V1/SOAP.svc

大部分文字未翻译。我需要你的建议才能继续前进。 我做的是正确的方式还是我需要改变的任何事情。任何帮助对我都有很大帮助。

提前致谢。

请参阅 NuGet 包 NequeoNetTranslator,其中包含文本和语音翻译 API。

文本翻译示例使用新的Cognitive版本,首先获取您的访问令牌,通过您的翻译[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();

现在调用翻译方法,这会将 en 翻译成 de:

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);

如果您使用语音转文本,则示例代码:

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();
    }
}