/// <summary>
 /// Facade for text translation
 /// </summary>
 /// <param name="text"></param>
 /// <param name="from"></param>
 /// <param name="to"></param>
 public void Translate(string text, string to, string from)
 {
     Initalize();
     try
     {
         //if (uiTranslatedTextBox != null)
         //    translatedTextBox = uiTranslatedTextBox;
         // Mr. Splashy pants was here!
         var query = context.Translate(text, to, from);
         query.BeginExecute(OnTranslateQueryComplete, query);
     }
     catch (DataServiceQueryException ex)
     {
         Debug.WriteLine("An error occurred during query execution." + ex.ToString());
         throw new Exception("An error occurred during query execution.", ex);
     }
 }
Example #2
0
        /// <summary>
        /// Uses the TranslatorContainer to translate inputString from sourceLanguage
        /// to targetLanguage.
        /// </summary>
        /// <param name="tc">The TranslatorContainer to use</param>
        /// <param name="inputString">The string to translate</param>
        /// <param name="sourceLanguage">The Language Code to use in interpreting the input string.</param>
        /// <param name="targetLanguage">The Language Code to translate the input string to.</param>
        /// <returns>The translated string, or null if no translation results were found.</returns>
        private static Translation TranslateString(TranslatorContainer tc, string inputString, DetectedLanguage sourceLanguage, Language targetLanguage)
        {
            // Generate the query
            var translationQuery = tc.Translate(inputString, targetLanguage.Code, sourceLanguage.Code);

            // Call the query and get the results as a List
            var translationResults = translationQuery.Execute().ToList();

            // Verify there was a result
            if (translationResults.Count() <= 0)
            {
                return(null);
            }

            // In case there were multiple results, pick the first one
            var translationResult = translationResults.First();

            return(translationResult);
        }
Example #3
0
        private string TranslateWithBing(string text, LanguageInfo from, LanguageInfo to, TranslateApi api)
        {
            if (!_bingTranslatorEnabled)
            {
                return(null);
            }

            if (api != TranslateApi.Bing && api != TranslateApi.All)
            {
                return(null);
            }

            string result;

            try
            {
                if (_bingTranslator == null)
                {
                    _bingTranslator =
                        new TranslatorContainer(new Uri(ConfigurationManager.AppSettings["TranslatorAddress"]))
                    {
                        Credentials = new NetworkCredential(
                            ConfigurationManager.AppSettings["TranslatorAccount"],
                            ConfigurationManager.AppSettings["TranslatorAccountKey"])
                    };

                    _bingTranslator.Timeout = (int)_bingTimeout;
                }

                var translationResult =
                    _bingTranslator.Translate(text, to.BingTranslatorCode, @from.BingTranslatorCode).ToList();

                result = translationResult.Select(t => t.Text).Aggregate((current, next) => current + ", " + next);
            }
            catch (Exception ex)
            {
                Log.LogException(ex);
                result = Resources.Res.TranslationError;
            }

            return(Resources.Res.Bing + result);
        }
        public string Translate(string translateMe, CultureInfo fromCultureInfo, CultureInfo toCultureInfo)
        {
            if (fromCultureInfo == null)
            {
                throw new ArgumentNullException("fromCultureInfo is null");
            }
            if (toCultureInfo == null)
            {
                throw new ArgumentNullException("toCultureInfo is null");
            }

            if (string.IsNullOrEmpty(translateMe))
            {
                return(translateMe);
            }

            string sourceLanguage = fromCultureInfo.TwoLetterISOLanguageName;
            string targetLanguage = toCultureInfo.TwoLetterISOLanguageName;

            TranslatorContainer tc = new TranslatorContainer(new System.Uri(SERVICE_URL));

            tc.Credentials = new System.Net.NetworkCredential(_username, _password);

            var translationQuery = tc.Translate(translateMe, targetLanguage, sourceLanguage);

            List <Microsoft.Translation> translationResults = translationQuery.Execute().ToList();

            // Verify there was a result
            if (translationResults.Count() <= 0)
            {
                return(null);
            }

            // In case there were multiple results, pick the first one
            Microsoft.Translation translationResult = translationResults.First();

            return(translationResult.Text);
        }
Example #5
0
        public string AutoTranslateSync(string untranslatedPhraseText, string untranslatedPhraseLanguage, string targetLanguageText)
        {
            //PREPARE THE TRANSLATOR CONTAINER (TRANSLATION SERVICE CLIENT)
            var serviceRootUri = new Uri(CommonResources.AzureServiceRootUriAddress);
//#if DEBUG
            var accountKey = CommonResources.LearnLanguagesAccountKey;
//#else
//      string accountKey = (string)System.Windows.Application.Current.Resources[CommonResources.AzureLearnLanguagesAccountKey];
//#endif
            TranslatorContainer translatorContainer = new TranslatorContainer(serviceRootUri);

            translatorContainer.Credentials = new NetworkCredential(accountKey, accountKey);

            //TURN THE LANGUAGE TEXTS INTO LANGUAGE CODES THAT SERVICE UNDERSTANDS
            var fromLanguageCode = TranslationHelper.GetLanguageCode(untranslatedPhraseLanguage);
            var toLanguageCode   = TranslationHelper.GetLanguageCode(targetLanguageText);

            //BUILDS THE TRANSLATION QUERY
            var translationQuery = translatorContainer.Translate(untranslatedPhraseText,
                                                                 toLanguageCode,
                                                                 fromLanguageCode);

            //ACTUALLY EXECUTE THE TRANSLATION QUERY
            var translations = translationQuery.Execute().ToList();

            //INTERPRET THE TRANSLATION RESULTS
            if (translations.Count > 0)
            {
                var translationText = translations.First().Text;
                //RETURN TRANSLATION (FIRST TRANSLATION, IF MULTIPLE EXIST)
                return(translationText);
            }
            else
            {
                //RETURN EMPTY STRING
                return(string.Empty);
            }
        }
Example #6
0
        private void translateItem(int itemId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(_data[itemId].Dialog))
                {
                    return;
                }

                LogWindow.AddMessage(LogType.Info, "Translating: " + _data[itemId].Dialog);

                var translatorContainer = new TranslatorContainer(ServiceUri)
                {
                    Credentials = new NetworkCredential(AccountKey, AccountKey)
                };
                var result = translatorContainer.Translate(_data[itemId].Dialog.Trim(), _targetLanguage, _sourceLanguage).Execute().ToList().FirstOrDefault();
                if (result == null)
                {
                    return;
                }

                _data[itemId].Dialog = result.Text;

                LogWindow.AddMessage(LogType.Info, "Result: " + _data[itemId].Dialog);
            }
            catch (Exception ex)
            {
                LogWindow.AddMessage(LogType.Error, ex.Message);
                if (ex.InnerException != null &&
                    ex.InnerException.Message.Contains("has an invalid pattern of characters"))
                {
                    LogWindow.AddMessage(LogType.Error, ex.InnerException.Message + " -> This language is not supported.");
                }
                StopTranslation = true;
            }
        }
Example #7
0
        //bing 번역기로부터 번역문 얻기
        private static Translation TranslateString(TranslatorContainer tc, string inputString, string transCode, string resultCode)
        {
            System.Data.Services.Client.DataServiceQuery <MORT.TransFormOver.Translation> translationQuery = tc.Translate(inputString, resultCode, transCode);

            // Call the query and get the results as a List
            System.Collections.Generic.List <MORT.TransFormOver.Translation> translationResults = translationQuery.Execute().ToList();

            // Verify there was a result
            if (translationResults.Count() <= 0)
            {
                return(null);
            }

            // In case there were multiple results, pick the first one
            Translation translationResult = translationResults.First();

            return(translationResult);
        }