Translate() публичный Метод

public Translate ( String Text, String To, String From ) : DataServiceQuery
Text String the text to translate Sample Values : hello
To String the language code to translate the text into Sample Values : nl
From String the language code of the translation text Sample Values : en
Результат DataServiceQuery
Пример #1
0
 private static string Translate(TranslatorContainer tc, string text, string to, string from)
 {
     var query = tc.Translate(text, to, from);
     var results = query.Execute().ToList();
     var result = results.First();
     return result.Text;
 }
Пример #2
0
        public static Microsoft.Translation TranslateString(TranslatorContainer tc, string text, Language source, Language dest)
        {
            var translationQuery = tc.Translate(text, source.Code, dest.Code);
            var translationResults = translationQuery.Execute().ToList();

            var translationResult = translationResults.FirstOrDefault();

            return translationResult;
        }
Пример #3
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);
        }
Пример #4
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;
      }
    }
        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;
        }