private async Task <List <string> > TranslateStringsUsingGoogleAsync(string language, string apiKey, string appName, List <string> strings) { string from = MultiString.GetPrimaryLanguage(MultiString.DefaultLanguage); string to = MultiString.GetPrimaryLanguage(language); List <string> newStrings = new List <string>(); int total = strings.Count(); int skip = 0; while (total > 0) { TranslateService service = new TranslateService(new BaseClientService.Initializer() { ApiKey = apiKey, ApplicationName = appName, }); TranslationsListResponse resp = await service.Translations.List(strings.Skip(skip).Take(40).ToList(), to).ExecuteAsync(); List <string> returnedStrings = (from r in resp.Translations select r.TranslatedText).ToList(); returnedStrings = (from r in returnedStrings select Utility.HtmlDecode(r)).ToList(); newStrings.AddRange(returnedStrings); skip += returnedStrings.Count(); total -= returnedStrings.Count(); } return(newStrings); }
public async void TranslateText() { //번역 서비스 생성 TranslateService service = new TranslateService(new BaseClientService.Initializer() { ApiKey = this.textBox3.Text, ApplicationName = " " }); //string[] srcText = new[] { "Hello world!", "The Google APIs client library for .NET uses client_secrets.json files for storing the client_id, client_secret, and other OAuth 2.0 parameters." }; try { //번역 요청 TranslationsListResponse response = await service.Translations.List(textBox1.Text, "ko").ExecuteAsync(); //결과를 저장 textBox2.Text = response.Translations[0].TranslatedText; } catch (Exception ex) { //api에서 문제가 생겨도 여기서 오류가 발생한다. //오류 내용을 확인해서 로그를 남겨야 할듯 Console.WriteLine("오류 :" + ex.ToString()); } }
void listen1() { srcText = textBox1.Text; String[] target_language_shortname = new String[] { "af", "sq", "am", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "ny", "zh-CN", "zh-TW", "co", "hr", "cs", "da", "nl", "en", "eo", "et", "tl", "fi", "fr", "fy", "gl", "ka", "de", "el", "gu", "ht", "ha", "haw", "iw", "hi", "hmn", "hu", "is", "ig", "id", "ga", "it", "ja", "jw", "kn", "kk", "km", "ko", "ku", "ky", "lo", "la", "lv", "lt", "lb", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn", "my", "ne", "no", "ps", "fa", "pl", "pt", "ma", "ro", "ru", "sm", "gd", "sr", "st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tg", "ta", "te", "th", "tr", "uk", "ur", "uz", "vi", "cy", "xh", "yi", "yo", "zu" }; var service = new TranslateService(new BaseClientService.Initializer() { ApiKey = "AIzaSyAsjrVKsPasvqfcZnvrIayprnql5zEIt0M", // your API key, that you get from Google Developer Console ApplicationName = "API key 1" // your application name, that you get form Google Developer Console }); TranslationsListResponse response = service.Translations.List(srcText, "en").Execute(); foreach (TranslationsResource t in response.Translations) { detect = t.DetectedSourceLanguage; } try { text = textBox1.Text; mp3path = Environment.CurrentDirectory + @"tmp.mp3"; wavpath = Environment.CurrentDirectory + @"\me\me.wav"; urltts = new Uri("http://translate.google.com/translate_tts?client=tw-ob&tl=" + detect + "-gb&q=" + text); using (tts = new WebClient()) { tts.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 9.0; Windows;)"); tts.DownloadFile(urltts, mp3path); } using (reader = new Mp3FileReader(new FileStream(mp3path, FileMode.OpenOrCreate))) { WaveFileWriter.CreateWaveFile(wavpath, reader); } SoundPlayer simpleSound = new SoundPlayer(@"C:\Users\Abdullah\Documents\Visual Studio 2013\Projects\LanguageTranslatorProject\LanguageTranslatorProject\bin\Debug\me\me.wav"); simpleSound.Play(); //MessageBox.Show("hogya"); } catch { MessageBox.Show("Error. Clear the screen first.\nNote that some languages are not supported."); } }
public static string translate(string english, string language) { string[] srcText = new[] { english }; ListRequest request = service.Translations.List(srcText, language); request.Source = "en"; TranslationsListResponse response = request.Execute(); String translated_text = response.Translations[0].TranslatedText; return(switchSingleQuoteToCurlyClosingQuote(translated_text)); }
private static IList <TranslationResult> UnpackTranslationResponse( List <string> inputs, string sourceLanguage, string targetLanguage, TranslationsListResponse response) { var translations = response.Translations; if (inputs.Count != translations.Count) { throw new InvalidOperationException($"Expected {inputs.Count} translation results; got {translations.Count}"); } return(inputs.Zip(translations, (input, translation) => new TranslationResult(translation, input, sourceLanguage, targetLanguage)) .ToList()); }
public virtual string Translate(string word, string toLanguage) { Google.Apis.Translate.v2.TranslateService service = new TranslateService(new BaseClientService.Initializer() { ApiKey = "AIzaSyCV2_E3yw4z8wg2xO1pw6U3GVFr3nytaKM" }); TranslationsListResponse result = service.Translations.List(new[] { word }, toLanguage).Execute(); return(result.Translations.First().TranslatedText); }
public string Translate(string text, string source, string dest) { var listRequest = TranService.Translations.List(text, dest); listRequest.Source = source; TranslationsListResponse response = listRequest.Execute(); string result = response.Translations .Select(t => t.TranslatedText) .ToArray()[0]; return(result); }
public string Translate(string text) { var svc = new TranslateService(new BaseClientService.Initializer { ApiKey = GetGoogleApiKey() }); SetLanguage(text); var listRequest = svc.Translations.List(text, TargetLangCode); listRequest.Key = "AIzaSyDz0FsFxxf7xsskqxlhaNWMvxM05b4HQBc"; TranslationsListResponse response = listRequest.Execute(); return(response.Translations.Select(t => t.TranslatedText).ToArray()[0]); }
public void DoTranslate(List <string> sources, ref List <string> translations) { var service = new TranslateService { Key = GetAPIKey() }; var list = service.Translations.List(sources, targetLanguage); list.Source = sourceLanguage; TranslationsListResponse response = list.Fetch(); foreach (TranslationsResource translation in response.Translations) { translations.Add(translation.TranslatedText); } }
//public string GetGoogleApiKey() //{ // return "AIzaSyCkwoW9Srl9ntpotdLNae2TWe2Wi98YFl8"; //} //public string GetGoogleSearchEngine() //{ // return "004317969426278842680:_f5wbsgg7xc"; //} public string Translate(string text, string targetLang) { var svc = new TranslateService(new BaseClientService.Initializer { ApiKey = CurrentApiKey, ApplicationName = CurrentApplicationName }); SetLanguage(text); var listRequest = svc.Translations.List(text, targetLang); //listRequest.Key = "AIzaSyDz0FsFxxf7xsskqxlhaNWMvxM05b4HQBc"; TranslationsListResponse response = listRequest.Execute(); return(response.Translations.Select(t => t.TranslatedText).ToArray()[0]); }
void willuse() { var service = new TranslateService(new BaseClientService.Initializer() { ApiKey = "AIzaSyAsjrVKsPasvqfcZnvrIayprnql5zEIt0M", // your API key, that you get from Google Developer Console ApplicationName = "API key 1" // your application name, that you get form Google Developer Console }); srcText = textBox1.Text; String[] target_language_shortname = new String[] { "af", "sq", "am", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "ny", "zh-CN", "zh-TW", "co", "hr", "cs", "da", "nl", "en", "eo", "et", "tl", "fi", "fr", "fy", "gl", "ka", "de", "el", "gu", "ht", "ha", "haw", "iw", "hi", "hmn", "hu", "is", "ig", "id", "ga", "it", "ja", "jw", "kn", "kk", "km", "ko", "ku", "ky", "lo", "la", "lv", "lt", "lb", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn", "my", "ne", "no", "ps", "fa", "pl", "pt", "ma", "ro", "ru", "sm", "gd", "sr", "st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tg", "ta", "te", "th", "tr", "uk", "ur", "uz", "vi", "cy", "xh", "yi", "yo", "zu" }; try { TranslationsListResponse response = service.Translations.List(srcText, target_language_shortname[comboBox2.SelectedIndex]).Execute(); foreach (TranslationsResource t in response.Translations) { textBox2.Text = t.TranslatedText; } } catch (IndexOutOfRangeException) { MessageBox.Show("Please select a language"); } catch (Google.GoogleApiException) { MessageBox.Show("Please enter the source text"); } }
public void NonMatchingTranslateResultCount() { var service = new FakeTranslateService(); var client = new TranslationClientImpl(service); var inputs = new string[] { "a", "b" }; var request = service.Translations.List(new Repeatable <string>(inputs), "en"); request.Format = FormatEnum.Text; var response = new TranslationsListResponse { // Content is irrelevant; we shouldn't get that far Translations = new List <TranslationsResource> { new TranslationsResource() } }; service.ExpectRequest(request, response); Assert.Throws <InvalidOperationException>(() => client.TranslateText(inputs, "en")); }
public override List <TranslationResult> Translate(TranslationCandidate candidates) { TranslateService service = new TranslateService(new BaseClientService.Initializer() { ApiKey = "AIzaSyCV2_E3yw4z8wg2xO1pw6U3GVFr3nytaKM" }); List <TranslationResult> trResult = new List <TranslationResult>(); foreach (string oneWord in candidates.Words) { TranslationsListResponse result = service.Translations.List(new[] { oneWord }, candidates.LanguageTo).Execute(); foreach (TranslationsResource translation in result.Translations) { trResult.Add(GetTranslationResult(translation, candidates, oneWord)); } } return(trResult); }
static void Main(string[] args) { // Initialize this sample. CommandLine.EnableExceptionHandling(); CommandLine.DisplayGoogleSampleHeader("Translate Sample"); // Ask for the user input. TranslateInput input = CommandLine.CreateClassFromUserinput <TranslateInput>(); // Create the service. var service = new TranslateService { Key = GetApiKey() }; // Execute the first translation request. CommandLine.WriteAction("Translating to '" + input.TargetLanguage + "' ..."); string[] srcText = new[] { "Hello world!", input.SourceText }; TranslationsListResponse response = service.Translations.List(srcText, input.TargetLanguage).Fetch(); var translations = new List <string>(); foreach (TranslationsResource translation in response.Translations) { translations.Add(translation.TranslatedText); CommandLine.WriteResult("translation", translation.TranslatedText); } // Translate the text (back) to english. CommandLine.WriteAction("Translating to english ..."); response = service.Translations.List(translations, "en").Fetch(); foreach (TranslationsResource translation in response.Translations) { CommandLine.WriteResult("translation", translation.TranslatedText); } // ...and we are done. CommandLine.PressAnyKeyToExit(); }
public void NonMatchingTranslateResultCount() { var service = new FakeTranslateService(); var client = new AdvancedTranslationClientImpl(service); var inputs = new string[] { "a", "b" }; var body = new TranslateTextRequest { Q = inputs, Format = "text", Target = "en" }; var request = service.Translations.Translate(body); var response = new TranslationsListResponse { // Content is irrelevant; we shouldn't get that far Translations = new List <TranslationsResource> { new TranslationsResource() } }; service.ExpectRequest(request, response); Assert.Throws <InvalidOperationException>(() => client.TranslateText(inputs, "en")); }