示例#1
0
        public string GetTranslate(string original, Language language)
        {
            _logger.Info($"[YandexTranslateService] Transslate of word: {original} was requested uses Yandex Translate");
            var url    = $"https://translate.yandex.net/api/v1.5/tr.json/translate";
            var apiKey = $"trnsl.1.1.20171204T105508Z.73de18b158ee992c.0732ee1e959ed65fefe5f904d935b7401fb2b0dc";

            using (var webClient = new WebClient())
            {
                var pars = new NameValueCollection
                {
                    { "key", apiKey },
                    { "text", original },
                    { "lang", language.ToString().ToLower() }
                };

                webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                var    response     = webClient.UploadValues(url, pars);
                string responsebody = Encoding.UTF8.GetString(response);

                var responseText = JObject.Parse(responsebody)["text"];
                var translate    = (string)responseText[0];
                if (language == Language.En)
                {
                    _repository.AddWord(translate, original);
                }
                else
                {
                    _repository.AddWord(original, translate);
                }
                return(translate);
            }
        }
示例#2
0
        public WordDictionary AddWord(string original, string translate)
        {
            _logger.Info($"[DictionaryService] The dictionary with original: {original} and translate: {translate} was request to add ");
            var requestResult = _repository.AddWord(original, translate);

            return(requestResult);
        }