예제 #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="from">en</param>
        /// <param name="to">el</param>
        /// <param name="accessToken"></param>
        /// <returns>#NLA# if there was a problem with the service OR the translated text</returns>
        public string TranslateText(HttpApplicationState app, string from, string to, string text)
        {
            string uri = "";
            try
            {
                AdmAccessToken admToken = new AdmAccessToken();
                if (app["BingTranslateAccessToken"] != null)
                {
                    admToken = (AdmAccessToken)app["BingTranslateAccessToken"];
                    int timeLeft = 0;
                    DateTime now = DateTime.Now;
                    DateTime tokenCreatedOn = (DateTime)app["BingTranslateAccessTokenCreatedDateTime"];
                    double difference = (now - tokenCreatedOn).TotalSeconds;

                    if ((now - tokenCreatedOn).TotalSeconds > 580)
                    {
                        RefreshAccessToken(app);
                    }
                }
                else
                {
                    RefreshAccessToken(app);
                }
                admToken = (AdmAccessToken)app["BingTranslateAccessToken"];

                string headerValue;
                //Get Client Id and Client Secret from https://datamarket.azure.com/developer/applications/
                //Refer obtaining AccessToken (http://msdn.microsoft.com/en-us/library/hh454950.aspx)

                // Create a header with the access_token property of the returned token
                //headerValue = "Bearer " + admToken.access_token;

                uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + System.Web.HttpUtility.UrlEncode(text) + "&from=" + from + "&to=" + to;
                string authToken = "Bearer" + " " + admToken.access_token;

                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
                httpWebRequest.Headers.Add("Authorization", authToken);

                WebResponse response = null;

                response = httpWebRequest.GetResponse();
                using (Stream stream = response.GetResponseStream())
                {
                    System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
                    string translation = (string)dcs.ReadObject(stream);
                    return translation;
                    //Console.WriteLine("Translation for source text '{0}' from {1} to {2} is", text, "en", "de");
                    //Console.WriteLine(translation);
                }
            }
            catch (Exception ex)
            {
                if (app["BingTranslateAccessToken"] != null)
                {
                    AdmAccessToken admToken = (AdmAccessToken)app["BingTranslateAccessToken"];
                    string errorInfo = "Token: " + admToken.access_token + " | Expires in: " + admToken.expires_in + " | Scope: " + admToken.scope + " | Token type: " + admToken.token_type + " | Uri: " + uri;

                    Ourspace_Utilities.View util = new Ourspace_Utilities.View();
                    util.AddTranslationErrorLogEntry(admToken.access_token, uri, admToken.expires_in, ex.Message);
                }
                else
                {
                    Ourspace_Utilities.View util = new Ourspace_Utilities.View();
                    util.AddTranslationErrorLogEntry("no token", uri, "no expires in", ex.Message);
                }
                string error = ex.Message;
                return "#NLA#" + error;
            }
        }