Ejemplo n.º 1
0
        public TranslateService.TranslateResponse.TranslationResult TranslateText(string source, string sourceLanguage, string targetLanguage)
        {
            if (!IsValidLanguageCode(targetLanguage))
            {
                throw new Exception("Invalid Target Language Code.");
            }
            else if (!IsValidLanguageCode(sourceLanguage))
            {
                throw new Exception("Invalid Source Language Code.");
            }

            source = Uri.EscapeDataString(source);
            var response = Client.GetAsync($"https://translate.yandex.net/api/v1.5/tr.json/translate?key={ApiKey}&text={source}&lang={sourceLanguage}-{targetLanguage}").Result;

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            var responseJson = response.Content.ReadAsStringAsync().Result;
            var token        = JToken.Parse(responseJson);

            var result = new TranslateService.TranslateResponse.TranslationResult();

            var text = token.Value <JArray>("text").FirstOrDefault().ToString();

            result.ApiType             = Models.TranslateConfig.ApiKey.Yandex;
            result.DestinationLanguage = targetLanguage;
            result.SourceLanguage      = sourceLanguage;
            result.SourceText          = source;
            result.TranslatedText      = TranslateService.FixTranslatedString(text);

            return(result);
        }
Ejemplo n.º 2
0
        public TranslateService.TranslateResponse.TranslationResult TranslateText(string source, string sourceLanguage, string targetLanguage)
        {
            if (TranslationClient == null)
            {
                return(null);
            }

            if (!IsValidLanguageCode(targetLanguage))
            {
                throw new Exception("Invalid Target Language Code.");
            }
            else if (!IsValidLanguageCode(sourceLanguage))
            {
                throw new Exception("Invalid Source Language Code.");
            }

            var response = TranslationClient.TranslateText(source, targetLanguage, sourceLanguage);
            var result   = new TranslateService.TranslateResponse.TranslationResult
            {
                SourceLanguage      = response.DetectedSourceLanguage,
                SourceText          = source,
                DestinationLanguage = response.TargetLanguage,
                TranslatedText      = TranslateService.FixTranslatedString(response.TranslatedText)
            };

            return(result);
        }
Ejemplo n.º 3
0
        public TranslateService.TranslateResponse.TranslationResult TranslateText(string source, string targetLanguage)
        {
            if (!IsValidLanguageCode(targetLanguage))
            {
                throw new Exception("Invalid Target Language Code.");
            }

            //TODO: fix source text for uri encoding.
            var response = Client.GetAsync($"https://translate.yandex.net/api/v1.5/tr.json/translate?key={ApiKey}&text={Uri.EscapeDataString(source)}&lang={targetLanguage}").Result;

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            var responseJson = response.Content.ReadAsStringAsync().Result;
            var token        = JToken.Parse(responseJson);

            var result = new TranslateService.TranslateResponse.TranslationResult();

            var lang      = token.Value <JToken>("lang").ToString();
            var splitChar = lang.IndexOf("-");

            //TODO: Default if split char is not found.
            var sourceLang = lang.Substring(0, splitChar);
            var destLang   = lang.Substring(splitChar + 1);
            var text       = token.Value <JArray>("text").FirstOrDefault().ToString();

            result.ApiType             = Models.TranslateConfig.ApiKey.Yandex;
            result.DestinationLanguage = destLang;
            result.SourceLanguage      = sourceLang;
            result.SourceText          = source;
            result.TranslatedText      = TranslateService.FixTranslatedString(text);

            return(result);
        }