Пример #1
0
        public TranslationResponse Translate(TranslationRequest translationRequest)
        {
            var client = new LanguageServiceClient();
            var translationResponse = new TranslationResponse();

            translationResponse.Success = false;
            try
            {
                translationResponse.Result =
                    client.Translate(TranslatorAppId, translationRequest.TextToTranslate,
                                     translationRequest.LanguageFrom, translationRequest.LanguageTo, "text/plain",
                                     "general");
                translationResponse.Success = true;
            }
            catch (Exception ex)
            {
                translationResponse.Error   = ex.ToString();
                translationResponse.Success = false;
            }
            return(translationResponse);
        }
Пример #2
0
        private IDictionary <string, string> GetLanguages(TranslationRequest translationRequest, bool addAdditionalLanguages)
        {
            const string apiUrl = "https://www.googleapis.com/language/translate/v2/languages?key={0}&target=en";
            var          url    = String.Format(
                apiUrl,
                TranslatorAppId);

            var dictionary = new Dictionary <string, string>();

            try
            {
                var req = WebRequest.Create(url);

                // set the request method
                req.Method = "GET";

                // get the response
                using (var res = req.GetResponse())
                {
                    if (res != null)
                    {
                        using (var sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8))
                        {
                            var json       = sr.ReadToEnd();
                            var jsonobject = JObject.Parse(json);
                            IEnumerable <JToken> results = jsonobject["data"]["languages"].Children();
                            dictionary = results.ToDictionary(result => result["language"].ToString(), result => result["name"].ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                dictionary.Add("ex", ex.Message);
            }
            return(dictionary);
        }
Пример #3
0
 public IDictionary <string, string> TargetLanguages(TranslationRequest translationRequest, bool addAdditionalLanguages)
 {
     return(GetLanguages(translationRequest, addAdditionalLanguages));
 }