Exemplo n.º 1
0
        private async Task DoTranslation()
        {
            try
            {
                string Identifer = Utility.RandomHex();
                DebugLog.Log("Making translation request [" + Identifer + "]: " + this.rawText);

                if (!File.Exists(Properties.Settings.Default.apiKeyPath))
                {
                    throw new FileNotFoundException("Keyfile not present at " + Properties.Settings.Default.apiKeyPath);
                }

                // Wait for rate limiter before starting the clock
                GoogleAsyncStatic.rate.Check();
                Stopwatch sw = new Stopwatch();

                // Make our connection client
                TranslationServiceClient translationServiceClient = new TranslationServiceClientBuilder
                {
                    CredentialsPath = Properties.Settings.Default.apiKeyPath,
                }.Build();

                // Request translation
                TranslateTextRequest request = new TranslateTextRequest
                {
                    //Contents = { rawText },
                    TargetLanguageCode   = Properties.Settings.Default.targetLocale,
                    ParentAsLocationName = new LocationName(Properties.Settings.Default.projectName, "global"),
                };

                // It does not appear that there's any way initialize the Contents above directly with a split of strings
                string[] splitters = new string[] { Environment.NewLine };
                request.Contents.AddRange(rawText.Split(splitters, StringSplitOptions.RemoveEmptyEntries));

                // Send request
                sw.Start();
                TranslateTextResponse response = await translationServiceClient.TranslateTextAsync(request);

                sw.Stop();

                // Anticipating one result per submitted line, in same order
                _translatedText = response.Translations
                                  .Select(tr => WebUtility.HtmlDecode(tr.TranslatedText))
                                  .Aggregate((l, r) => l + Environment.NewLine + r);

                // Close enough
                _detectedLocale = response.Translations.First().DetectedLanguageCode;

                _timeStamp = string.Format("{0:00}:{1:00}:{2:00}.{3:000}",
                                           sw.Elapsed.Hours,
                                           sw.Elapsed.Minutes,
                                           sw.Elapsed.Seconds,
                                           sw.Elapsed.Milliseconds);

                isDone = true;

                callback?.Invoke(this);
                DebugLog.Log("Finishing translation [" + Identifer + "]: " + this._translatedText);
            }
            catch (Grpc.Core.RpcException e)
            {
                Form.Invoke(Form.SafeLogWorkerError, new object[] { e.Message, "http://www.yahoo" });
            }
            catch (Exception e)
            {
                Form.Invoke(Form.SafeLogWorkerError, new object[] { e.Message, "" });
            }
        }
Exemplo n.º 2
0
        private async Task DoGSL()
        {
            try
            {
                string Identifer = Utility.RandomHex();
                DebugLog.Log("Making Google get supported languages request [" + Identifer + "]");

                if (!File.Exists(Properties.Settings.Default.googleApiKeyPath))
                {
                    throw new FileNotFoundException("Keyfile not present at " + Properties.Settings.Default.googleApiKeyPath);
                }

                // Wait for rate limiter before starting the clock
                AsyncStatic.rate.Check();
                Stopwatch sw = new Stopwatch();

                // Make our connection client
                TranslationServiceClient translationServiceClient = new TranslationServiceClientBuilder
                {
                    CredentialsPath = Properties.Settings.Default.googleApiKeyPath,
                }.Build();

                // Request supported languages
                GetSupportedLanguagesRequest request = new GetSupportedLanguagesRequest
                {
                    DisplayLanguageCode  = "en",
                    ParentAsLocationName = new LocationName(Properties.Settings.Default.googleProjectName, "global"),
                };

                // Send request
                sw.Start();
                SupportedLanguages response = await translationServiceClient.GetSupportedLanguagesAsync(request);

                sw.Stop();

                // Convert these to a format the combo box will like
                _languages = response.Languages
                             .Where(lang => lang.SupportTarget)
                             .Select(ConvertLanguage)
                             .ToArray();

                _timeStamp = string.Format("{0:00}:{1:00}:{2:00}.{3:000}",
                                           sw.Elapsed.Hours,
                                           sw.Elapsed.Minutes,
                                           sw.Elapsed.Seconds,
                                           sw.Elapsed.Milliseconds);

                isDone = true;
                callback?.Invoke(this);

                DebugLog.Log("Finishing google getting supported languages [" + Identifer + "]");
            }
            catch (Grpc.Core.RpcException e)
            {
                frmBabel.LogWorkerError(e.Message, "");
            }
            catch (Exception e)
            {
                frmBabel.LogWorkerError(e.Message, "");
            }
        }