Пример #1
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);
        }
Пример #2
0
        /// <summary>
        /// Translate a text
        /// </summary>
        /// <param name="accountKey">Bing account key</param>
        /// <param name="sourceLng">source language</param>
        /// <param name="targetLng">target language</param>
        /// <param name="inputString">text to translate</param>
        /// <param name="outputString">translated text</param>
        /// <returns>error code:
        /// 0 : translation successful
        /// -1: inputstring is empty
        /// -2: text cannot be translated (invalid source language or not detected source language)
        /// -3: error during translation
        /// </returns>
        public static int TranslateMethod(String accountKey, string sourceLng, string targetLng, string inputString, out string outputString)
        {
            outputString = "";
            // Call GetInputString to manage the input
            //inputString = "This is a lovely slice of text that I'd like to translate";//GetInputString(args);

            // Verify that the user actually provided some input
            if (inputString == "")
            {
                //Console.WriteLine("Usage: RandomTranslator.exe Some Input Text");
                return(-1);
            }

            // Improved support for unicode
            //Console.OutputEncoding = Encoding.UTF8;

            // Print the input string
            //Console.WriteLine("Input: " + inputString);

            //Console.WriteLine();

            // Call a InitializeTranslatorContainer to get a TranslatorContainer
            // that is configured to use your account.
            TranslatorContainer tc = InitializeTranslatorContainer(accountKey);

            // DetectSourceLanguage encapsulates the work required for language
            // detection
            DetectedLanguage sourceLanguage = null;

            if ((sourceLng == null) || (sourceLng == ""))
            {
                sourceLanguage = DetectSourceLanguage(tc, inputString);
            }
            else
            {
                sourceLanguage      = new DetectedLanguage();
                sourceLanguage.Code = sourceLng;
            }

            // Handle the error condition
            if (sourceLanguage == null)
            {
                //Console.WriteLine("\tThis string cannot be translated");

                return(-2);
            }

            // Print the detected language code
            //Console.WriteLine("Detected Source Language: " + sourceLanguage.Code);

            //Console.WriteLine();

            // PickRandomLanguage encapsulates the work required to detect all
            // languages supported by the service and then to pick one at random
            var targetLanguage = PickRandomLanguage(tc);

            targetLanguage.Code = targetLng;

            // Print the selected language code
            //Console.WriteLine("Randomly Selected Target Language: " + targetLanguage.Code);

            //Console.WriteLine();

            // TranslateString encapsulates the work required to translate
            // a string from the source language to the target language
            var translationResult = TranslateString(tc, inputString, sourceLanguage, targetLanguage);

            // Handle the error condition
            if (translationResult == null)
            {
                //Console.WriteLine("Translation Failed.");
                return(-3);
            }

            // Print the result of the translation
            //Console.WriteLine("Translated String: " + translationResult.Text);
            outputString = translationResult.Text;
            return(0);
        }