コード例 #1
0
        /// <inheritdoc />
        public void TranslateText(TranslateTextEventHandler eventHandler, string key, string textToTranslate, string languageFrom, string languageTo)
        {
            if (IsInitialized == false)
            {
                Debug.LogError("The translator is not initialized! Aborting process");
                return;
            }
            if (eventHandler == null)
            {
                Debug.LogError("There's no valid event handler set to translate text! Aborting process");
                return;
            }
            if (textToTranslate == string.Empty || languageFrom == string.Empty || languageTo == string.Empty)
            {
                Debug.LogError("Invalid values to translate! Aborting process");
                return;
            }

            Debug.Log("Translating key:" + key + ", from:" + languageFrom + ", to:" + languageTo);

            string     url = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + WWW.EscapeURL(textToTranslate) + "&from=" + languageFrom + "&to=" + languageTo;
            WebRequest translationWebRequest = HttpWebRequest.Create(url);

            translationWebRequest.Headers["Authorization"] = headerValue;


            TranslateTextData translateTextData = new TranslateTextData();

            translateTextData.dictionaryKey         = key;
            translateTextData.translationWebRequest = translationWebRequest;
            translateTextData.eventHandler          = eventHandler;

            translationWebRequest.BeginGetResponse(new AsyncCallback(ReadTranslateTextResponse), translateTextData);
        }
コード例 #2
0
        void ReadTranslateTextResponse(IAsyncResult asyncResult)
        {
            TranslateTextData translationData = null;
            HttpWebRequest    request         = null;
            HttpWebResponse   response        = null;

            try
            {
                translationData = asyncResult.AsyncState as TranslateTextData;
                request         = translationData.translationWebRequest as HttpWebRequest;
                response        = request.EndGetResponse(asyncResult) as HttpWebResponse;

                using (Stream streamResponse = response.GetResponseStream())
                {
                    string translatedValue = string.Empty;
                    using (XmlReader reader = XmlReader.Create(streamResponse))
                    {
                        reader.Read();
                        reader.Read();
                        translatedValue = reader.Value;
                    }

                    Debug.Log("Successfully translated key:" + translationData.dictionaryKey + "!");
                    if (translationData.eventHandler != null)
                    {
                        translationData.eventHandler(true, translationData.dictionaryKey, translatedValue);
                    }
                    translationData.eventHandler = null;
                }
            }
            catch (WebException exception)
            {
                Debug.LogError("Failed to translate text! error: " + exception.Message);
                using (Stream streamResponse = exception.Response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(streamResponse))
                    {
                        Debug.LogError(reader.ReadToEnd());
                    }
                }
                if (translationData.eventHandler != null)
                {
                    translationData.eventHandler(false, string.Empty, string.Empty);
                }
                translationData.eventHandler = null;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
            }
        }