Exemplo n.º 1
2
        public string Translate(string Text, string targetlan)
        {
            try
            {
                LanguagesListResponse ls = new LanguagesListResponse();
                Google.Apis.Translate.v2.Data.LanguagesResource ss = new Google.Apis.Translate.v2.Data.LanguagesResource();
                if (targetlan == "en-US") targetlan = "en";
                ss.Language = targetlan;

                // GetLanguageCode

                var service = new TranslateService(new BaseClientService.Initializer()
                {
                    ApiKey = googlekey
                });
                //new TranslateService { Key = googlekey };
                ICollection<string> data = new Collection<string>();
                string[] srcText = new[] { Text };
                TranslationsListResponse response = service.Translations.List(srcText, targetlan).Execute();
                var translations = new List<string>();

                // We need to change this code...
                // currently this code
                foreach (Google.Apis.Translate.v2.Data.TranslationsResource translation in response.Translations)
                {
                    translations.Add(translation.TranslatedText);
                }
                string translated = translations[0];
                log.InfoFormat("[GoogleTranslate] Text={0}, targetlan={1}, translation={2}.", Text, targetlan, translated);
                if (translated != null && translated.IndexOf("&#39;") > 0)
                {
                    translated = translated.Replace("&#39;", "'");
                }
                return translated;
            }
            catch (Exception ex)
            {
                log.ErrorFormat("[Translate] Exception={0}.", ex);
                return null;
            }
        }
Exemplo n.º 2
1
        public string GoogleTranslate(string Text, string targetlan)
        {
            try
            {
                LanguagesListResponse ls = new LanguagesListResponse();
                Google.Apis.Translate.v2.Data.LanguagesResource ss = new Google.Apis.Translate.v2.Data.LanguagesResource();
                ss.Language = targetlan;

                // GetLanguageCode
                string googlekey = "AIzaSyCILytJnsn0FjzG6L7siOPeAqXTa9cQ0A8";

                var service = new TranslateService(new BaseClientService.Initializer()
                {
                    ApiKey = googlekey
                });
                //new TranslateService { Key = googlekey };
                ICollection<string> data = new Collection<string>();
                string[] srcText = new[] { Text };
                TranslationsListResponse response = service.Translations.List(srcText, targetlan).Execute();
                var translations = new List<string>();

                // We need to change this code...
                // currently this code
                foreach (Google.Apis.Translate.v2.Data.TranslationsResource translation in response.Translations)
                {
                    translations.Add(translation.TranslatedText);
                }
                log.InfoFormat("[GoogleTranslate] Text={0}, targetlan={1}, translation={2}.", Text, targetlan, translations[0]);
                return translations[0];
            }
            catch (Exception ex)
            {
                log.ErrorFormat("[GoogleTranslate] Exception={0}.", ex);
                return null;
            }
        }