Example #1
0
        private static Dictionary <string, string> GetCulturesDataSource()
        {
            Dictionary <string, string> cultureDataSource = new Dictionary <string, string>();

            string[] languageCodes = null;
            string[] languageNames = null;

            if (HasValidMTSCredentials && Utilities.FormUtility.UseMTS)
            {
                try
                {
                    TranslatorService.TranslateOptions options = new TranslatorService.TranslateOptions();

                    using (OperationContextScope scope = new OperationContextScope(MTSLanguageServiceClient.InnerChannel))
                    {
                        OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = MTSAccessHeader;
                        //Keep appId parameter blank as we are sending access token in authorization header.
                        languageCodes = MTSLanguageServiceClient.GetLanguagesForTranslate("");
                        languageNames = MTSLanguageServiceClient.GetLanguageNames(BingAppId, CultureInfo.CurrentUICulture.Name, languageCodes);
                    }
                }
                catch (WebException wex)
                {
                    LogUtility.WriteError(wex.ToString());
                }
                catch (Exception ex)
                {
                    LogUtility.WriteError(ex.ToString());
                }
            }
            else if (HasValidAppId && Utilities.FormUtility.UseBing)
            {
                languageCodes = MTSLanguageServiceClient.GetLanguagesForTranslate(BingAppId);
                languageNames = MTSLanguageServiceClient.GetLanguageNames(BingAppId, CultureInfo.CurrentCulture.Name, languageCodes);
            }

            if (languageCodes != null && languageCodes.Length > 0 && languageNames != null && languageNames.Length > 0 && languageCodes.Length == languageNames.Length)
            {
                for (int i = 0; i < languageNames.Length; i++)
                {
                    cultureDataSource.Add(languageCodes[i], languageNames[i]);
                }
            }
            else
            {
                cultureDataSource = new Dictionary <string, string>();
            }

            return(cultureDataSource);
        }
Example #2
0
        public static bool ValidateAppId(string bingAppId)
        {
            try
            {
                if (string.IsNullOrEmpty(bingAppId))
                {
                    return(false);
                }
                string[] texts = new string[] { "welcome", "hello" };

                TranslatorService.TranslateOptions options = new TranslatorService.TranslateOptions();

                TranslatorService.TranslateArrayResponse[] translatedTexts =
                    MTSLanguageServiceClient.TranslateArray(bingAppId, texts, "en", "fr", options);

                return(true);
            }
            catch
            {
            }

            return(false);
        }