Ejemplo n.º 1
0
        public WordsListObject GetTranslateFromYandex(List <string> list)
        {
            List <Word>     words_list      = null;
            TranslateObject translateObject = null;
            WordsListObject wordsListObject = null;
            string          keyApi          = "trnsl.1.1.20170126T141135Z.59c382f4c439bbaf.3226485ac0b3691894625bddb3c48c74f5067706";
            string          format          = "plain";
            string          lang            = "en-ru";
            string          text            = "";

            foreach (var item in list)
            {
                text += "&text=" + WebUtility.UrlEncode(item);
            }
            string    url    = "https://translate.yandex.net/api/v1.5/tr.json/translate?key=" + keyApi + text + "&lang=" + lang + "&[format=" + format + "]&[options=1]";
            Uri       uri    = new Uri(url);
            WebClient client = new WebClient
            {
                Encoding = Encoding.UTF8
            };
            string stringData = "";

            try
            {
                Stream       data   = client.OpenRead(uri);
                StreamReader reader = new StreamReader(data);
                stringData = reader.ReadToEnd();
                reader.Close();
                translateObject = JsonConvert.DeserializeObject <TranslateObject>(stringData);
                if (translateObject != null && list.Count == translateObject.text.Count)
                {
                    words_list      = new List <Word>();
                    wordsListObject = new WordsListObject();
                    for (int i = 0; i < list.Count; i++)
                    {
                        words_list.Add(new Word
                        {
                            Eng      = list.ElementAt(i),
                            Rus      = translateObject.text.ElementAt(i),
                            IsRepeat = true
                        });
                    }
                    wordsListObject.code      = translateObject.code;
                    wordsListObject.lang      = translateObject.lang;
                    wordsListObject.wordsList = words_list;
                }
            }
            catch (Exception err)
            {
                Debug.WriteLine(err.Message);
            }
            return(wordsListObject);
        }
        public async Task <dynamic> TranslateObject([FromBody] TranslateObjectModel objModel)
        {
            TranslateObject translateObject = _mapper.Map <TranslateObject>(objModel);

            return(await _translateObjectServiceAdapter.TranslateObjectTask(translateObject));
        }