Пример #1
0
        public List <Pair> GetSupportedLanguages()
        {
            BingTranslate.LanguageServiceClient client = CreateWebServiceInstance();

            string[] languageCodes = null;
            string[] languageNames = null;

            // Creates a block within which an OperationContext object is in scope.
            using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
            {
                OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = PrepareClientForPOST();
                //Keep appId parameter blank as we are sending access token in authorization header.
                languageCodes = client.GetLanguagesForTranslate("");
            }

            using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
            {
                OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = PrepareClientForPOST();
                //Keep appId parameter blank as we are sending access token in authorization header.
                languageNames = client.GetLanguageNames("", "en", languageCodes, false);
            }

            List <Pair> langList = new List <Pair>();

            for (int i = 0; i < languageNames.Length; i++)
            {
                langList.Add(new Pair(languageCodes[i], languageNames[i]));
            }
            langList.Add(new Pair("AUTO", "AUTO"));
            return(langList);
        }
Пример #2
0
        public string Translate(string from, string to, string text)
        {
            BingTranslate.LanguageServiceClient client = CreateWebServiceInstance();


            // Creates a block within which an OperationContext object is in scope.
            using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
            {
                OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = PrepareClientForPOST();
                String result = client.Translate("", text, from.Equals("AUTO")?"":from, to, "text/plain", "general");

                return(result);
            }
        }