Exemple #1
0
        public List <string> Translate(string sourceLang, string targetLang, string text)
        {
            var response = WClient.UploadString(@$ "https://translation.googleapis.com/language/translate/v2?key={ApiKey}", JsonSerializer.Serialize(new TranslateRequest(text, sourceLang, targetLang, "text")));

            var deserialized = JsonSerializer.Deserialize <TranslateResponse>(response);
            var result       = new List <string>();

            foreach (var item in deserialized.data.translations)
            {
                result.Add(item.translatedText);
            }
            return(result);
        }
Exemple #2
0
        public string DetectLanguage(string text)
        {
            var response = WClient.UploadString($"https://translation.googleapis.com/language/translate/v2/detect?key={ApiKey}", JsonSerializer.Serialize(new DetectLanguageRequest()
            {
                q = text
            }));
            var    deserialized  = JsonSerializer.Deserialize <DetectLanguageResponse>(response);
            double maxConfidence = 0;
            string targetLang    = null;

            foreach (var detection in deserialized.data.detections)
            {
                foreach (var lang in detection)
                {
                    if (lang.confidence > maxConfidence)
                    {
                        maxConfidence = lang.confidence;
                        targetLang    = lang.language;
                    }
                }
            }
            return(targetLang);
        }